如果在数组foreach中存在ID,则对于每个键=" id" PHP

时间:2015-02-23 13:50:14

标签: php arrays find

编辑:

我试图使用Alex的方法,但我遇到了一些问题...所以我得到第一个数组为$ resultsAchievementsMe,就像这样:

$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);

print_r,会写下这个:

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 ) )

这个数组,将包含多于1个成就,用户将解锁多个...

现在,用这个:

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

输出很简单:

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

这个数组,包含更多信息,我的意思是更多成就,因为如果用户解锁一个新的,当他重新启动应用程序时,这个数组将有另一个具有相同方法(id),(名称成就)

现在,这个另一个数组,已经完成了所有的成就,我得到了类似的方法:

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

$resultsAchievements = $requestAchievements ->asArray();

print_r($resultsAchievements);

print_r的输出如下:

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 ) ) )

这个数组有更多的信息,比如成就描述,图片的网址......等等......但采用与以前类似的方法:

$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.'"]],';
 }

我会得到这个:

[["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"]],

现在,您可以看到,用户已从第一个阵列解锁了test2 id(6467916820996)...但我想只制作一个数组,并在第二个数组中插入一个新值以查看所有ID为每个成就解锁...所以,最终的输出将是:

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

我尝试Alex的这种方法,但不起作用......

$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.'"],["';
  }
有人有点想法吗?我不能使用Ghost的例子,因为它会改变输出的字符

谢谢你:)

1 个答案:

答案 0 :(得分:0)

我的目标不是100%明确。

恕我直言,您的代码应该重新设计。

但是如果你需要快速修复,你确定$resultsAchievementsMe只包含1个元素而不是你可以尝试这一行:

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

如果你在$resultsAchievementsMe中有很多,你应该使用循环(或创建一些函数):

    $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'.']';
  }

顺便说一句,我不明白你是否真的将arrayjson混合使用[]{},但是如果{}则使用json } mean object - 不是数组

无论如何,我更希望得到更详细的目标描述,更复杂的数据源样本,并重新设计这个片段,使用json作为json,数组作为数组,对象作为对象。