简体   繁体   English

如何打印出PHP数组的唯一值?

[英]How to print out the unique values of a PHP array?

Details 细节

  • I have a list of my country in an array called $country 我在名为$country的数组中列出了我的国家/ $country
  • When I do dd($country); 当我做dd($country); I see this 我看到这个

    在此处输入图片说明

  • I want to print out all country name of that array ( unique value only ) 我想打印出该数组的所有国家/地区名称(仅唯一值)

  • can someone show me how to print that ? 有人可以告诉我如何打印吗?

Here is what I've tried 这是我尝试过的

I tried echo (array_unique(array_values($country))); 我试过了echo (array_unique(array_values($country)));

But I got an error 但是我有一个错误

ErrorException (E_UNKNOWN) 
Array to string conversion

You can't echo an array. 您无法echo数组。 It's not a string. 不是字符串。

// There's no need to use array_values
$uniqueValues = array_unique($continent);

foreach($uniqueValues as $country){
    echo $country, '<br>';
}

Since I have my array from $countries[$k] = $v['hq_country']['name']; 由于我的数组来自$countries[$k] = $v['hq_country']['name'];

Printing them out, I just need to do this 打印出来,我只需要这样做

foreach(array_unique($countries) as $country){ 

echo $country ;

} 

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

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