[英]PHP Select Unique Values from Array
Using PHP and MySQL I have generated an array (from an API lookup). 使用PHP和MySQL,我已经生成了一个数组(通过API查找)。 A
var_dump
of the array can her seen here . 她可以在这里看到数组的
var_dump
。
I would like to return only unique 'title' values from this array. 我只想从此数组返回唯一的“ title”值。
My code so far is; 到目前为止,我的代码是;
foreach($response["Items"]["Item"] as $item)
{
$title = $item["ItemAttributes"]["Title"];
}
A var_dump
of $title
produces; 产生
$title
的var_dump
;
string(5) "Panic" string(5) "Panic" string(73) "Captain Flinn and the Pirate Dinosaurs: Missing Treasure! (Captain Flinn)"
You can see that there are two string values named "Panic". 您可以看到有两个名为“ Panic”的字符串值。
I have tried array_unique
but can't seem to get it working. 我试过
array_unique
但似乎无法正常工作。
Any help is appreciated. 任何帮助表示赞赏。 Thanks
谢谢
Try with this trick: if title is already present it will not added. 尝试使用此技巧:如果标题已经存在,则不会添加。
$titlesFounded = [];
foreach($response["Items"]["Item"] as $item) {
if (!in_array($item["ItemAttributes"]["Title"], $titlesFounded)) {
$titlesFounded[] = $item["ItemAttributes"]["Title"];
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.