我是更新的学习php。我在php facebook people search how to remove repeat value?
中提出了我的问题但仍有一些问题想要了解。
这是数据示例。
https://graph.facebook.com/search?q=tom&type=user&access_token=2227470867|2.AQD2FG3bzBMEiDV3.3600.1307905200.0-100001799728875|LowLfLcqSZ9YKujFEpIrlFNVZPQ
我希望清除重复值。这是Tomalak Geret'kal的答案,谢谢他。它可以清除重复名称。
$names = Array();
foreach ($status_list['data'] as $data) {
$names[] = $data['name'];
}
$names = array_unique($names); // not print the same name.
foreach ($names as $name) {
echo $name;
}
因为我仍想回复$id
。我在这里累了2个方法:
$names = Array();
foreach ($status_list['data'] as $data) {
$names[] = $data['name'];
$id[] = $data['id'];
}
$names = array_unique($names); // not print the same name.
foreach ($names as $name) {
echo $name;
echo $id; // no result
}
$names = Array();
foreach ($status_list['data'] as $data) {
$names[] = $data['name'];
}
$names = array_unique($names); // not print the same name.
foreach ($names as $name) {
echo $name;
echo $data['id']; // all the id is the last people.
}
如何做正确的方法?感谢。
答案 0 :(得分:2)
这没有任何意义。
您只有重复项,因为您丢弃了“名称”以外的所有信息,这不是一个独特的字段。
当您将id
字段( 唯一)添加到图片中时,结果中不再有任何重复项。
所以,请写下:
foreach ($status_list['data'] as $data) {
echo $data['name'] . " " . $data['id'] . "\n";
}
答案 1 :(得分:0)
试试这个:
$data = array_unique($status_list['data']);
foreach ($data as $i) {
echo $i['name'];
echo $i['id'];
}
通过使用$names = array_unique($names);
,您只将所有名称放在数组$names
中。
foreach ($status_list['data'] as $data) {
$names[] = $data['name'];
$ids[] = $data['id'];
}
$names = array_unique($names); // not print the same name.
$ids = array_unique($ids); // not print the same name.
$i = 0
foreach ($names as $name) {
echo $name;
echo $ids[$i];
$i ++;
}
答案 2 :(得分:0)
$names = Array();
$names=array("0"=>1,"1"=>"1","2"=>"2","3"=>"2","4"=>"23"); //same value in array
$names = array_unique($names); // not print the same name.
foreach ($names as $name) {
echo "\n\r".$name;
}