如何使用php从数组中获取唯一值

如何使用php从数组中获取唯一值,php,arrays,multidimensional-array,Php,Arrays,Multidimensional Array,这里有一个数组 Array ( [0] => stdClass Object ( [gallery_id] => 25 [title] => Our Job is Your Vacation [image_path] => 1.png ) [1] => stdClass Object ( [gallery_id] =>

这里有一个数组

 Array
(
   [0] => stdClass Object
       (
           [gallery_id] => 25
           [title] => Our Job is Your Vacation
           [image_path] => 1.png
       )

   [1] => stdClass Object
       (
           [gallery_id] => 25
           [title] => Our Job is Your Vacation
           [image_path] => 2.png
       )

   [2] => stdClass Object
       (
           [gallery_id] => 25
           [title] => Our Job is Your Vacation
           [image_path] => 3.jpg
       )

   [3] => stdClass Object
       (
           [gallery_id] => 26
           [title] => enjoy vacation
           [image_path] => 1.jpg
       )
   [4] => stdClass Object
       (
           [gallery_id] => 26
           [title] => enjoy vacation
           [image_path] => 2.jpg
       )
)
预期的数组为

   Array
    (
       [0] => stdClass Object
           (
               [gallery_id] => 25
               [title] => Our Job is Your Vacation
               [image_path] => array{
                                      '1.png',
                                      '2.png',
                                      '3.png'
                                    }
           )
       [1] => stdClass Object
           (
               [gallery_id] => 26
               [title] => enjoy vacation
               [image_path] => array{
                                      '1.jpg',
                                      '2.jpg'
                                     }
           )
    }
我怎么能得到这个,有人能帮我吗

编辑 以下是表格结构

表1-画廊

**gallery_id**        **title**
'25'              'Our Job is Your Vacation'
'26'              'enjoy vacation'
表2-画廊图片

**id**     **gallery_id**        **image_path**
1            '25'              '1.png'
2            '25'              '2.png'
3            '25'              '3.png'
4            '26'              '1.jpg'
5            '26'              '2.jpg'

我不确定,但这样的查询可能会对您的图像路径进行分组:

SELECT title,
GROUP_CONCAT(DISTINCT image_path SEPARATOR ',') as image_path
FROM gallery_images JOIN gallery ON gallery_images.gallery_id = gallery.id
GROUP BY student_name;
然后在PHP中,您必须
分解(“,”,$image\u path)


这不是最终的解决方案,但我希望这能帮助您

我认为这是一种糟糕的方法,最好从数据库中正确格式化数据。但是试试这个

其中$youArray是您的旧数组

<?php 

$newArray = array();
//put here your array instead of $yourArray
foreach ($youArray as $key => $value) { 
  if(empty($newArray)) {
    array_push($newArray,$value);
    continue;
  }
  foreach ($newArray as $k => $v) {

    if($v->gallery_id == $value->gallery_id) {

      if($v->image_path == $value->image_path) {
        continue;
      } else {
        if(is_array($newArray[$k]->image_path)) {
          array_push(
            $newArray[$k]->image_path,
            $value->image_path
          );
        } else {
          $newArray[$k]->image_path = array(
            $newArray[$k]->image_path,
            $value->image_path
          );
        }
      }
    } else {
      $repeated = 0;
      foreach ($newArray as $ky => $val) {
        if($newArray[$ky]->gallery_id == $value->gallery_id) {
          $repeated ++ ;
        }
      }
      if(!$repeated) {
        array_push($newArray,$value);
      }
    }
  }
}

var_dump($newArray);

您是如何将其保存在数据库中的?我的意思是您的表的格式是什么???@Drudge我已经编辑了问题,请再次检查。我确信您可以通过更改SQL查询来完成此操作。尝试使用GROUP CONCAT等@Adam i want query in codeignitercan u post您使用的查询??请注意,
GROUP_CONCAT
有一个由
GROUP_CONCAT\u max_len
系统变量设置的最大长度(我认为它默认为1024个字符)。