0

我有以下json:

{
  "data": [
    {
      "id": "116376655202954/insights/page_impressions_organic/day", 
      "name": "page_impressions_organic", 
      "period": "day", 
      "values": [
        {
          "value": 13059, 
          "end_time": "2013-02-22T08:00:00+0000"
        }, 
        {
          "value": 15223, 
          "end_time": "2013-02-23T08:00:00+0000"
        },

我使用 PHP 来获取以下 foreach 的valueend_time

foreach ($page_impressions_organic['data'] as $page_impression_organic) {
    foreach($page_impression_organic['values'] as $key => $page_impression_organic_values) {
        $end_time = strval($page_impression_organic_values['end_time']);
        $ds = substr($end_time, 0, -14);
        $value = intval($page_impression_organic_values['value']);
        echo '[\'' . $ds . '\', ' . $value . ']';
}}

一切正常,我的问题是现在我有一个不同的 json:

{
  "data": [
    {
      "id": "116376655202954/insights/page_storytellers_by_story_type/day", 
      "name": "page_storytellers_by_story_type", 
      "period": "day", 
      "values": [
        {
          "value": {
            "fan": 2043, 
            "page post": 293, 
            "user post": 5
          }, 
          "end_time": "2013-02-22T08:00:00+0000"
        }, 
        {
          "value": {
            "fan": 1085, 
            "page post": 398, 
            "user post": 5
          }, 
          "end_time": "2013-02-23T08:00:00+0000"
        }, 

而且我无法获得相同的结果,因为value它是一个数组,我特别需要page post它的值。我需要保留我的回声具有的形式以用于进一步的范围。我为此做了另一个 foreach:

foreach ($page_talking_abouts['data'] as $page_talking_about) {
    foreach($page_talking_about['values'] as $key => $page_talking_about_values) {
        foreach($page_talking_about_values['value'] as $page_talking_about_value) {
            $end_time = strval($page_talking_about_values['end_time']);
            $ds = substr($end_time, 0, -14);
            $value = intval($page_talking_about_value['page post']);
            echo '[\'' . $ds . '\', ' . $value . ']';
}}}

$value每次都是 0... 我必须做什么?

4

3 回答 3

2

它很简单。查看重写的代码:

foreach ($page_talking_abouts['data'] as $page_talking_about) {
    foreach($page_talking_about['values'] as $key => $page_talking_about_values) {
        $end_time = strval($page_talking_about_values['end_time']);
        $ds = substr($end_time, 0, -14);
        $value = intval($page_talking_about_values['value']['page post']);
        echo '[\'' . $ds . '\', ' . $value . ']';
    }
}

如果您只需要一个已知索引,您真的不必遍历内部值数组。您的解决方案中发生的情况是,对于嵌套值数组中的每个,您都尝试获取 index page post,但由于嵌套数组的值不是数组,因此page post返回的索引 a如果输入,则null转换为。0intval

除此之外,我真的建议将您的变量重命名为更具可读性的名称。那些带有细微差异的长名称使代码难以阅读。

于 2013-04-22T13:17:08.813 回答
1

我认为您不需要第三个 for 循环,因为每个value数组只存在一次end-time

尝试像这样更改您的代码:

foreach ($page_talking_abouts['data'] as $page_talking_about) {
    foreach($page_talking_about['values'] as $key => $page_talking_about_values) {

        // note: removed third for-loop

        $end_time = strval($page_talking_about_values['end_time']);
        $ds = substr($end_time, 0, -14);

        // note: changed this line to directly read out the page posts
        $value = intval($page_talking_about_values['value']['page post']);

        echo '[\'' . $ds . '\', ' . $value . ']';
}}
于 2013-04-22T13:13:08.543 回答
0

您不必直接循环遍历它。使用 json_decode 解码字符串,然后执行 foreach 循环,然后再次对其进行 json_encode

于 2013-04-22T13:03:54.433 回答