dongmi5015 2015-02-23 13:50
浏览 88

如果在数组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 :)

  • 写回答

1条回答 默认 最新

  • dongshuohuan5291 2015-02-23 14:15
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题