【问题标题】:JSON Response Multidimensional ArrayJSON 响应多维数组
【发布时间】:2014-08-18 13:49:45
【问题描述】:

您好,我收到了如下所示的 JSON 响应。我想计算早于 24 小时的帖子,并检查唯一用户网址:

{  
   "meta":{  
      "network":"all",
      "query_type":"realtime"
   },
   "posts":[  
      {  
         "network":"facebook",
         "posted":"2014-08-16 08:31:31 +00000",
         "sentiment":"neutral",
         "url":"someURL",
         "user":{  
            "name":"Terance Podolski",
            "url":"someURL",
            "image":"someURL"
         }
      },
      {  
         "network":"facebook",
         "posted":"2014-08-16 08:30:44 +00000",
         "sentiment":"neutral",
         "url":"someURL",
         "user":{  
            "name":"Łukasz Podolski",
            "url":"someURL",
            "image":"someURL"
         }
      },
      {  
         "network":"facebook",
         "posted":"2014-08-16 08:25:39 +00000",
         "sentiment":"neutral",
         "url":"someURL",
         "user":{  
            "name":"Marcin Podolski",
            "url":"someURL",
            "image":"someURL"
         }
      }
]
}

提前致谢。

在@Elias Van Ootegem 的帮助下,我的问题得到了解决。代码如下:

// Json Reponse decodieren
$jsonArray = json_decode($jsonData);



function getMentionsFromLast24H($myArray){
    // set variable exactly one day ago
    $since = new DateTime('-1 day');

    // array where to store timestamps in
    $recent = array();

    foreach ( $myArray -> posts as $post ) {

        try {
            $post -> posted = new DateTime (substr ( $post->posted,0,19 ) );//create DateTime instance
            if ( $post -> posted >= $since )
                $recent[] = $post;//add to array
        } catch ( Exception $e ) {
            echo $e -> getMessage();
            exit(1);
        }

    }

    return $recent;

}

$mentions24h = count(getMentionsFromLast24H($jsonArray));

print_r($mentions24h);

【问题讨论】:

标签: php arrays json multidimensional-array


【解决方案1】:

真的很简单:解码 json 数据,将 posted 值与时间 - 24 小时进行比较,如果值大于 time-24 小时,则将其添加到数组中。就是这样,您最终会得到一个包含过去 24 小时内添加的所有帖子的数组:

$data = json_decode($jsonData);//creates object
$since = new DateTime('yesterday');
$recent = array();//this is the array we'll be constructing
foreach ($data->posts as $post)
{
    $post->posted = new DateTime($post->posted);//create DateTime instance
    if ($post->posted > $since)
        $recent[] = $post;//add to array
}
var_dump($recent);//this is the array you're after

这就是它的全部。

【讨论】:

  • 我得到了一个空数组 (size=0)
  • @fabu_baracus:这样的话,从昨天开始就没有帖子了
  • 当我尝试使用您的示例时,循环似乎没有循环。由于多维数组,我是否可能必须在彼此内部使用两个循环???
  • @fabu_baracus:如果响应确实像您在此处显示的示例,那么不是:帖子数组在$jsonDecodedData->posts 中找到,该数组的每个元素都是stdClass 的一个实例,它的时间戳位于属性posted 下。请注意,您在此处发布的数据列出了 1 天之前的帖子(08/16 是 2 天前)
  • 如果你使用我贴在这里的代码,那应该是json_decode($jsonData),没有第二个参数(true),函数被称为json_decode,而不是@987654329 @...您可以继续将数据解码为数组,但用数组索引替换属性访问位(foreach ($data['posts'] as $post) 而不是$data->posts)。还要确保包含 json 字符串的变量实际上被称为 $jsonData
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 2015-11-12
  • 1970-01-01
  • 1970-01-01
  • 2018-03-28
  • 2017-08-10
  • 1970-01-01
相关资源
最近更新 更多