如果在数组foreach中存在ID,则为每个key =“id”PHP

EDIT:

I'm trying to use the method of Alex, but I have some problem...so I get the first array as $resultsAchievementsMe, like this:

$requestAchievementsMe = (new \Facebook\FacebookRequest( $session, 'GET', '/me/achievements?fields=data&app_id_filter=7606995873455'))->execute()->getGraphObject(\Facebook\GraphUser::className());

$resultsAchievementsMe = $requestAchievementsMe ->asArray();
$tests = $resultsAchievementsMe['data'];

print_r($tests);

the print_r, will write this:

Array ( [0] => stdClass Object ( [data] => stdClass Object ( [importance] => 0 [achievement] => stdClass Object ( [id] => 6467916820996 [url] => https://www.***.com/test2.html [type] => game.achievement [title] => TITLE2!! ) ) [id] => 10206139019499208 ) )

this array, would contain more than 1 achievement, the user will unlock more than one...

now, with this:

     if (empty($resultsAchievementsMe)) {
        echo 'no achievements unlocked for now';
      }else {
        foreach ($resultsAchievementsMe['data'] as $tests) {
$totalAchievementsMe .= '[["' . $tests->data->achievement->id .'"],["'. $tests->data->achievement->title.'"]],';
     }  
    }

the output wuold be simple:

[["6467916820996"],["TEST2!!"]],

This array, would contain more information, I means, more achievement, because if the user unlock a new one, when he restart the app, this array will have another one with the same method (id),(name achievement).

Now, this another array, has all of the achievment, and I get with the similar method:

$requestAchievements = (new \Facebook\FacebookRequest( $session, 'GET', '/7606995873455/achievements'))->execute()->getGraphObject(\Facebook\GraphUser::className());

$resultsAchievements = $requestAchievements ->asArray();

print_r($resultsAchievements);

the output of print_r, would be like this:

Array ( [0] => stdClass Object ( [url] => https://www.***.com/test2.html [type] => game.achievement [title] => TITLE2!! [image] => Array ( [0] => stdClass Object ( [url] => https://www.***.com/test2.png [width] => 198 [height] => 198 ) ) [description] => description achievement2 [updated_time] => 2015-02-22T14:51:54+0000 [data] => stdClass Object ( [points] => 100 ) [id] => 6467916820996 [application] => stdClass Object ( [id] => 7606995873455 [name] => myapp [url] => https://apps.facebook.com/myapp/ ) [context] => stdClass Object ( [display_order] => 0 ) ) [1] => stdClass Object ( [url] => https://www.***.com/test1.html [type] => game.achievement [title] => TEST1!! [image] => Array ( [0] => stdClass Object ( [url] => https://www.***.com/test1.png [width] => 198 [height] => 198 ) ) [description] => description achievement1 [updated_time] => 2015-02-22T14:51:46+0000 [data] => stdClass Object ( [points] => 50 ) [id] => 7829964917383 [application] => stdClass Object ( [id] => 7606995873455 [name] => EatSquare [url] => https://apps.facebook.com/myapp/ ) [context] => stdClass Object ( [display_order] => 0 ) ) )

This array, has more information, like the description of the achievment, the url of the picture...etc... but with the similar method as before:

$expic = "png";
foreach ($resultsAchievements as $result) {

   $totalAchievements .= '[["' . $result->data->points .'"],["'. $UNLOCKED? .'"],["'. $result->title .'"],["'. $result->description .'"],["'. $result->id .'"],["'.  $result->url = substr($result->url, 0, -4).$expic.'"]],';
 }

I will get this:

[["100"],["TEST2!!"],["VARIABLE I NEED TO MAKE**"],["description achievement2"],["6467916820996"],["https://www.***.com/test2.png"]],[["50"],["TEST1!!"],["VARIABLE I NEED TO MAKE**"],["description achievement1"],["7829964917383"],["https://www.***.com/test1.png"]],

now, you can see, the user has the test2 id (6467916820996) unlocked from the first array... but I would like to make just one array, and insert a new value in the second one to see the all ID for each achievement unlocked...so, the final output will be like:

[["100"],["TEST2!!"],["UNLOCKED**"],["description achievement2"],["6467916820996"],["https://www.***.com/test2.png"]],[["50"],["TEST1!!"],["LOCKED!!!**"],["description achievement1"],["7829964917383"],["https://www.***.com/test1.png"]],

I try this method of Alex, but doesn't works...

$testid=$result->id;
    $exist=false;


    foreach($resultsAchievementsMe['data'] as $resultsAchievements){
       if($testid ==  $result->id) {  
          $totalAchievements .= $result->data->points .'"],["'. $result->title .'"],["'. 'UNLOCKED' .'"],["'. $result->description .'"],["'. $result->id .'"],["'.  $result->url .'"],';
          $exist=true;
          break;
      }
  }
  if ($exist == false) {
     $totalAchievements .=$result->data->points .'"],["'. $result->title .'"],["'. 'LOCKED!!!' .'"],["'. $result->description .'"],["'. $result->id .'"],["'.  $result->url.'"],["';
  }

someone has some idea? I can't use the example of Ghost because will change the characters of the output

thank you :)

Your goal is not 100% clear for me.

IMHO your code should be redesigned.

But if you need quick fix and you are sure that $resultsAchievementsMe include just 1 element than you can try this line:

$testid=$result->id;
if($testid == $resultsAchievementsMe['data'][0]['data']['achievement']['id'])) {   

and if you have many in $resultsAchievementsMe you should use loop (or create some function):

    $testid=$result->id;
    $exist=false;
    foreach($resultsAchievementsMe['data'] as $myAchievement){
       if($testid ==  $myAchievement['data']['achievement']['id'])) {  
          $totalAchievements .= $result->data->points .'"],["'. $result->title .'"],["'. $result->description .'"],["'. $result->id .'"],["'.  $result->url'"],["'.'EXIST'.']';
          $exist=true;
          break;
      }
  }
  if ($exist == false) {
     $totalAchievements .=$result->data->points .'"],["'. $result->title .'"],["'. $result->description .'"],["'. $result->id .'"],["'.  $result->url'"],["'.'DOESN'T EXIST'.']';
  }

and by the way I don't understand if you really mix array with json you use [] and {} but in json if {} mean object - not array

anyway, I would prefer to get more detailed goal description, more complicated data source samples and and redesign this fragment to use json as json, array as array and objects as object.