在json文件中计算记录?

时间:2011-07-13 19:17:00

标签: php json facebook

我想知道如何计算json中记录中的记录。

如果你看这里,你可以看到我所拥有的文字的一个例子。

http://russellhopedesign.com/facebook/toplikedstatus/data.js

现在。我想看看哪个唱片最“喜欢”

我是否必须做一些For Each然后计数?

1 个答案:

答案 0 :(得分:0)

编辑:每次与OP进行对话时重新编辑。

我会循环开始,循环遍历所有数据。有一个变量来存储你想要保存的那个。然后对于每个索引,为每个循环做一个,检查属性并查看它是否等于“喜欢”。每当你找到一个有喜欢属性的,获取长度并保存它

$json = json_decode(file_get_contents('https://graph.facebook.com/100002367437090/statuses?limit=100000000&access_token=145634995501895|c19bad6810f80442a09516c5.0-100002367437090|wcIUgp977G3_nSiJTCF3mY_PMis'), true);        
    $data = $json['data'];
    $mostLiked = null;
    $saved = false;

    for($i = 0; $i < count($data); $i++){
      foreach($data[$i] as $key => $value){
        //echo "property is => " . $key . '<br>';
        if($key == "likes"){
          //echo "********* a like is here";
          //echo '<br><hr>';
          //echo 'No. of likes => ' . count($data[$i]['likes']['data']) . '<br>';
          if(!$saved){
            //echo "*** nothing saved yet, save what we've just found <br>";
            $mostLiked = $data[$i];
            $saved = true;
          }else if($saved && (count($data[$i]['likes']['data']) > count($mostLiked['likes']['data']))){
            //echo "*** this one is bigger than what was saved before <br>";
            $mostLiked = $data[$i];
          };
        };
      };
    };
    //var_dump($mostLiked);
    echo 'This post has the most likes with => ' . count($mostLiked['likes']['data']);