0

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.

3 Answers 3

2

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'] . "\n";
}
Sign up to request clarification or add additional context in comments.

2 Comments

As I say, I just for study. pls do not think the real station. If I want compare name no repeat, then print all the json item data, name, id ... if there will have more. How to do?
no matter, do not stand in the real situation, but stand in technical. how to judge in a foreach like this. compare one part is not repeat, then print all the data in this dom. Thanks.
0

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 ++;
}

3 Comments

$status_list['data'] already contains no dups. The OP, for some reason, wants to collapse the dataset so that there's one entry per name, maintaining the map from that name to one of the IDs it represents. (And I refuse to write code for that because it's meaningless!)
make up to +0 . I have tried add array_unique in $status_list['data'], but just get the first name and id. I think may be store them all into array then do the next step, but I am newer, not know how to do. Thanks.
@fishman: Do not vote for answers that you do not understand just because somebody else downvoted them.
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;
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.