如何在PHP中返回基于JSON结果的唯一列表?
我试图使用一个JSON链接中的值填充其他JSON链接中的值,从而生成一个来自组合JSON的唯一值列表。我已经创建了完整的值列表,但是我很难在最后的循环中找到只显示唯一值的语法。任何帮助都将不胜感激如何在PHP中返回基于JSON结果的唯一列表?,php,json,unique,Php,Json,Unique,我试图使用一个JSON链接中的值填充其他JSON链接中的值,从而生成一个来自组合JSON的唯一值列表。我已经创建了完整的值列表,但是我很难在最后的循环中找到只显示唯一值的语法。任何帮助都将不胜感激 $collection_string = file_get_contents("http://some_json_url.com/json"); $collection_list = json_decode($collection_string, true); foreach ($collectio
$collection_string = file_get_contents("http://some_json_url.com/json");
$collection_list = json_decode($collection_string, true);
foreach ($collection_list as $col_lists => $col_list) {
$object_string = file_get_contents('http://another_json_url.com/'.$col_list['key'].'/json');
$object_list = json_decode($object_string, true);
$object_array = array();
foreach ($object_list as $objects => $object) {
$object_array [] = array_unique($object); //Returns "Warning: array_unique() expects parameter 1 to be array, string given in..."
echo '<li><a href="some_search_url/'.$object_array.'/search/">'.$object_array.'</a></li>'; //Returns "Array"
echo '<li><a href="some_search_url/'.$object.'/search/">'.$object.'</a></li>'; //Returns complete list
}
}
$collection\u string=file\u get\u contents('http://some_json_url.com/json");
$collection\u list=json\u decode($collection\u string,true);
foreach($col\u list作为$col\u list=>$col\u list的集合列表){
$object\u string=文件\u获取\u内容('http://another_json_url.com/“.$col_list['key']./json”);
$object\u list=json\u decode($object\u string,true);
$object_array=array();
foreach($object_列表为$objects=>$object){
$object_array[]=array_unique($object);//返回“警告:array_unique()要求参数1为数组,字符串在…中给定”
echo“”;//返回“Array”
echo“”;//返回完整列表
}
}
工作代码:
$collection_string = file_get_contents("http://some_json_url.com/json");
$collection_list = json_decode($collection_string, true);
$object_array = array();
foreach ($collection_list as $col_lists => $col_list) {
$object_string = file_get_contents('http://another_json_url.com/'.$col_list['key'].'/json');
$object_list = json_decode($object_string, true);
foreach ($object_list as $key => $value) {
array_push($object_array, $value);
}
}
$object_unique = array_unique($object_array);
natcasesort($object_unique);
foreach ($object_unique as $key => $value) {
echo '<li><a href="some_search_url/'.$value.'/search/">'.$value.'</a></li>';
}
$collection\u string=file\u get\u contents('http://some_json_url.com/json");
$collection\u list=json\u decode($collection\u string,true);
$object_array=array();
foreach($col\u list作为$col\u list=>$col\u list的集合列表){
$object\u string=文件\u获取\u内容('http://another_json_url.com/“.$col_list['key']./json”);
$object\u list=json\u decode($object\u string,true);
foreach($object\u列表为$key=>$value){
数组\u push($object\u数组,$value);
}
}
$object\u unique=array\u unique($object\u array);
natcasesort($object_unique);
foreach($object\u唯一为$key=>$value){
回音“”;
}
只需更改此选项即可
$object_array [] = array_unique($object);
到那
$object_array [] = $object; // edited !
array_unique($object_array);
也许你也可以用一行代码来完成,但我不知道怎么写。但是我写它的方式有点未优化,最好只在最后一个循环之后执行一次array_unique()
顺便说一句,您的问题是/曾经试图使$object唯一,而不是数组。这是一个字符串/对象。感谢您的快速响应。不幸的是,我仍然得到以下信息:“警告:array_unique()期望参数1为array,字符串给定…在第42行array”(重复)我刚刚发现一个错误并编辑了我的帖子。请看一看!你必须删除“你的”数组。\u unique。编辑处理了我看到的错误。现在的响应是“ArrayArrayArray…”,而不是每个数组的值,但我不知道为什么;返回“数组(1){[0]=>string(5)”image”}!您需要遍历它,比如
foreach($array as$element){echo$element;}