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

时间:2013-11-21 12:13:20

标签: 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] => 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'
                                     }
           )
    }

我怎么能得到这个,任何人都可以帮助我。

修改 以下是表格结构

table1 - gallery

**gallery_id**        **title**
'25'              'Our Job is Your Vacation'
'26'              'enjoy vacation'

table2 - gallery_images

**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'

2 个答案:

答案 0 :(得分:3)

我不确定但是这样的查询可以对你的image_path进行分组:

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中你必须explode(",",$image_path);

这不是最终的解决方案,但我希望这会对你有所帮助

答案 1 :(得分:2)

我认为这是一种可怕的方式,最好从数据库中获取正确的数据。但试试这个。

$ 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);