I am newer studing php. I have asked my question in php facebook people search how to remove repeat value?
but still have some problem want to understand well.
here is the data example.
https://graph.facebook.com/search?q=tom&type=user&access_token=2227470867|2.AQD2FG3bzBMEiDV3.3600.1307905200.0-100001799728875|LowLfLcqSZ9YKujFEpIrlFNVZPQ
I want clear the repeat values. here is the answer form Tomalak Geret'kal, thanks him. It can clear the repeat name.
$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;
}
for I still want to echo $id
. I tired 2 method here:
$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.
}
How to do the right method? Thanks.
Try this:
$data = array_unique($status_list['data']);
foreach ($data as $i) {
echo $i['name'];
echo $i['id'];
}
By using $names = array_unique($names);
you only put all the names in the array $names
.
EDIT: well i agree with Tomalak Geret'kal, cause it makes no sense to echo the ID but you could try the following:
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 ++;
}
This makes no sense.
You only had duplicates because you were discarding all information other than "Name", which was not a unique field.
When you bring the id
field (which is unique) into the picture, you no longer have any duplicates in the result.
So, just write:
foreach ($status_list['data'] as $data) {
echo $data['name'] . " " . $data['id'] . "
";
}
$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 "
".$name;
}