简体   繁体   English

PHP仅从数组中打印出唯一值

[英]PHP print out only unique values from array

I'm hoping someone can help me with a problem I am having returning only unique values from an array. 我希望有人可以解决我从数组中仅返回唯一值的问题。

I am pulling data from the 500px API of photographs that I have favourited. 我正在从我喜欢的照片的500px API中提取数据。 From this array I would like to pull out the category IDs of the photos. 我想从这个数组中提取照片的类别ID。 The below does the job to a certain extent, but I want to show only unique values (ultimately I want to build a navigation from the labels associated with the IDs). 下面在一定程度上可以完成这项工作,但是我只想显示唯一的值(最终我想从与ID关联的标签构建一个导航)。

if($json){
        $obj = json_decode($json); 
                }
      else {
        print "<p>Currently, No Service Available.</p>";
            } 


            foreach ($obj->photos as $photo){


        print $photo->category;

           } 

This just returns a string 99241210812231382611121281221121812. These are the correct category IDs of the photos I have favourited however I would like to show 9, 1, 2 etc only once. 这只是返回字符串99241210812231382611121281221121812。这些是我收藏的照片的正确类别ID,但是我只想显示9、1、2等一次。

I've spent some time on the PHP manual and on here and have tried the below 我花了一些时间在PHP手册上和此处,并尝试了以下内容

if($json){
        $obj = json_decode($json); 
                }
      else {
        print "<p>Currently, No Service Available.</p>";
            } 


            foreach ($obj->photos as $photo){
              $test=$photo->category;
             $unique=str_split($test);
print array_unique($unique);

           }

But this just returns a sting ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray 但这只会返回一个字符串ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray

If I try: 如果我尝试:

foreach ($obj->photos as $photo){

print array_unique($photo->category);

           }

I get Warning: array_unique() [function.array-unique]: The argument should be an array. 我得到警告:array_unique()[function.array-unique]:参数应为数组。 Any help would be much appreciated! 任何帮助将非常感激!

Set an array to store the categories. 设置一个数组来存储类别。 Fill it with category ids. 用类别ID填充它。 Make it hold only unique values. 使它仅包含唯一值。 Echo it. 回声吧。

$categories=array();
foreach ($obj->photos as $photo){
    $categories[]=$photo->category;
}
$categories=array_unique($categories);

print implode(', ', $categories); // will show a string 9, 1, 2

print_r($categories); // will show the values as an array 
                      // you may want to view page source to read them easily

http://uk3.php.net/function.implode http://uk3.php.net/function.implode
http://uk1.php.net/print_r http://uk1.php.net/print_r

Another way to do this is by setting array keys $categories[$photo->category]=true; 另一种方法是通过设置数组键$categories[$photo->category]=true; and then using array_keys() . 然后使用array_keys()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM