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


how get unique values from an array using php

这里有一个数组

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

我不确定,但这样的查询可能会将您的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);

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

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

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