php从多维(json响应)数组构建更简单的数组

时间:2016-05-30 18:33:27

标签: php json facebook api multidimensional-array

我正在尝试从Facebook响应中获取的多维数组中的数据到更简单的数组。

json数据如下所示。

{
"albums": {
    "data": [
        {
            "photos": {
                "data": [
                    {
                        "name": "a photo name",
                        "source": "https://fbcdn-sphotos-d-a.akamaihd.net/hphotos-ak-xlp1/t31.0-8/s720x720/XXXXXXXXXXXX",
                        "picture": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xfp1/v/t1.0-0/XXXXXXXXXXXXXX",
                    },
                    {
                        "name": "a photo name",
                        "source": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xtp1/v/t1.0-0/p480x480/XXXXXXXXXXXXXXXXXX",
                        "picture": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xtp1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
                    },
                    {
                        "name": "a photo name",
                        "source": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xtf1/v/t1.0-0/p480x480/XXXXXXXXXXXXXX",
                        "picture": "https://fbcdn-photos-d-a.akamaihd.net/hphotos-ak-xtf1/v/t1.0-0/s130x130/XXXXXXXXXXXXXXX",
                    },
                    {
                        "name": "a photo name",
                        "source": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-0/p480x480/XXXXXXXXXXXXXXXXXX",
                        "picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
                    }
                ],
                "paging": {
                    "cursors": {
                        "before": "MTXXXXXXXXXXXXXXXZD",
                        "after": "MXXXXXXXXXXXXXZD"
                    }
                }
            },
        },
        {
            "photos": {
                "data": [
                    {
                        "source": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xfp1/v/t1.0-9/XXXXXXXXXXXXXXX",
                        "picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xfp1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
                    },
                    {
                        "name": "XXXXXXXXX",
                        "source": "https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/XXXXXXXXXXX",
                        "picture": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
                    },
                    {
                        "name": "XXXXXXXXXXX",
                        "source": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/XXXXXXXXXXX",
                        "picture": "https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-0/p130x130/XXXXXXXXXXX",
                    },
                    {
                        "name": "a photo name",
                        "source": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXXXXX",
                        "picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/v/t1.0-0/s130x130/XXXXXXXXXXXX",
                    },
                    {
                        "name": "a photo name",
                        "source": "https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-xpa1/t31.0-0/XXXXXXXXXXX",
                        "picture": "https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-xpt1/v/t1.0-0/XXXXXXXXXX",
                    },
                    {
                        "name": "a photo name",
                        "source": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xaf1/t31.0-0/p480x480/XXXXXXXXXX",
                        "picture": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xat1/v/t1.0-0/s130x130/XXXXXXXX",
                    },
                    {
                        "name": "a photo name",
                        "source": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX",
                        "picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/v/t1.0-0/s130x130/XXXXXXXX",
                    }
                ],
                "paging": {
                    "cursors": {
                        "before": "MTXXXXXXXXXXXXXXXgZDXXXX",
                        "after": "MTcXXXXXXXXXXXXXXXXXD"
                    }
                }
            },
        },
        {
            "photos": {
                "data": [
                    {
                        "source": "https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xta1/v/t1.0-9/1",
                        "picture": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xta1/v/t1.0-0/s130x130/",
                    },
                    {
                        "name": "XXXXXXXXXXXXXXXXX",
                        "source": "https://scontent.xx.fbcdn.net/v/t1.0-9/11",
                        "picture": "https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-xaf1/v/t1.0-0/s130x130/11",
                    }
                ],
                "paging": {
                    "cursors": {
                        "before": "MOIXXXXXXXXXXXXX",
                        "after": "MTXXXXXXXXXXXXXD"
                    }
                }
            },
        },
        {
            "photos": {
                "data": [
                    {
                        "source": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xpl1/t31.0-8/s720x720/",
                        "picture": "https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xft1/v/t1.0-0/s130x130/",
                    }
                ],
                "paging": {
                    "cursors": {
                        "before": "MTXXXXXXXXXXXSJKSDJZDZD",
                        "after": "MTdXXXXXXXXDKSJKDNXXXXXXXXXpZD"
                    }
                }
            },
        }
    ],
    "paging": {
        "cursors": {
            "before": "MXXXXXXXXXXXXXXD",
            "after": "MXXXXXXXXXXXXJR"
        }
    }
},
"feed": {
    "data": [
        {
        },
        {
            "attachments": {
                "data": [
                    {
                        "media": {
                            "image": {
                                "height": 480,
                                "src": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xpf1/t31.0-8/s720x720/",
                                "width": 720
                            }
                        }
                    }
                ]
            }
        },
        {
            "attachments": {
                "data": [
                    {
                        "media": {
                            "image": {
                                "height": 102,
                                "src": "https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xta1/v/t1.0-9/",
                                "width": 197
                            }
                        }
                    }
                ]
            }
        },
        {
            "attachments": {
                "data": [
                    {
                        "media": {
                            "image": {
                                "height": 276,
                                "src": "https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xpl1/t31.0-8/s720x720/",
                                "width": 720
                            }
                        }
                    }
                ]
            }
        },
        {
        },
        {
            "attachments": {
                "data": [
                    {
                        "description": "XXXXXXXXXXXXKE",
                        "media": {
                            "image": {
                                "height": 540,
                                "src": "https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/",
                                "width": 540
                            }
                        }
                    }
                ]
            }
        },
        {
            "attachments": {
                "data": [
                    {
                        "description": "XXXXXXXXXXJ",
                        "media": {
                            "image": {
                                "height": 375,
                                "src": "https://scontent.xx.fbcdn.net/v/t1.0-9/",
                                "width": 500
                            }
                        }
                    }
                ]
            }
        },
        {
            "attachments": {
                "data": [
                    {
                        "description": "XXXXXXXXXXXX",
                        "media": {
                            "image": {
                                "height": 540,
                                "src": "https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xaf1/v/t1.0-0/p180x540/",
                                "width": 720
                            }
                        }
                    }
                ]
            }
        },
        {
            "attachments": {
                "data": [
                    {
                        "description": "XXXXXXXXXXXXp",
                        "media": {
                            "image": {
                                "height": 405,
                                "src": "https://scontent.xx.fbcdn.net/v/t1.0-9/s720x720/",
                                "width": 720
                            }
                        }
                    }
                ]
            }
        },
        {
            "attachments": {
                "data": [
                    {
                        "media": {
                            "image": {
                                "height": 392,
                                "src": "https://scontent.xx.fbcdn.net/v/t1.0-9/",
                                "width": 626
                            }
                        }
                    }
                ]
            }
        },
        {
            "attachments": {
                "data": [
                    {
                        "description": "XXXXXXXX",
                        "media": {
                            "image": {
                                "height": 255,
                                "src": "https://fbcdn-sphotos-e-a.akamaihd.net/hphotos-ak-xpa1/v/t1.0-9/",
                                "width": 208
                            }
                        }
                    }
                ]
            }
        }
    ],
    "paging": {
        "previous": "https://graph.facebook.com/v2.5/1XXXXXXXXXXXX6/feed?fields=a",
        "next": "https://graph.facebook.com/v2.5/1SXXXXXXXXXXXXX96/feed?fields="
    }
},
}

我想最终得到一个像这样的数组:

Array
(
[photo] => Array
    (
        [0] => Array
            (
                [description] => a photo name
                [image_src] => https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
            )
        [1] => Array
            (
                [description] => a photo name
                [image_src] => https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
            )
        [2] => Array
            (
                [description] => a photo name
                [image_src] => https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
            )

            and so on......
    )
)

但我明白了:

Array
(
[photo] => Array
    (
        [0] => Array
            (
                [description] => a photo name
            )

        [1] => Array
            (
                [image_src] => https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
            )
        [2] => Array
            (
                [description] => a photo name
            )

        [3] => Array
            (
                [image_src] => https://fbcdn-photos-b-a.akamaihd.net/hphotos-ak-xpf1/t31.0-0/p480x480/XXXXXX
            )
and so on...

)
)

这是我当前循环的代码:

foreach ($raw_facebook['albums']['data'] as $photos) { 
         if ($photos) {
                foreach ($photos['photos']['data'] as $photo) {
                    if ( (isset($photo['name']) && array_key_exists('name', $photo)) && (isset($photo['source']) && $this->array_key_exists_recursive('source', $photo)) ) {
                        $facebook['photo'][]['description'] = $photo['name'];
                        $facebook['photo'][]['image_src'] = $photo['source'];
                    }
                }

            }
}

当我将此代码修改为:

foreach ($raw_facebook['albums']['data'] as $photos) { 
            if ($photos) {
                $i = 0;
                foreach ($photos['photos']['data'] as $photo) {
                    if ( (isset($photo['name']) && array_key_exists('name', $photo)) && (isset($photo['source']) && $this->array_key_exists_recursive('source', $photo)) ) {
                        $facebook['photo'][$i]['description'] = $photo['name'];
                        $facebook['photo'][$i]['image_src'] = $photo['source'];
                    }
                }
                $i++;

            }
}

我得到的结果与我需要的结果类似,但只有6个内部数组(当应该有10个时),显示迭代不正确。

请欣赏指向正确的方向。

3 个答案:

答案 0 :(得分:1)

这是:array_push($facebook['photo'], array( 'description' => $photo['name'], 'image_src' => $photo['source'] ));

$facebook['photo'] = array();
foreach ($raw_facebook['albums']['data'] as $photos) { 
    if ($photos){
        foreach($photos['photos']['data'] as $photo) {
            if((isset($photo['name']) && array_key_exists('name', $photo)) && (isset($photo['source']) && $this->array_key_exists_recursive('source', $photo)) ){
                array_push($facebook['photo'], array( 'description' => $photo['name'], 'image_src' => $photo['source'] ));
            }
        }

    }
}

答案 1 :(得分:1)

你去吧

$data = $raw_facebook['albums']['data']; //indexed array of objects
$output = array('photo'    =>   array());
for($i = 0; $i< count($data); $i++){
    $photosdataarray = $data[$i]['photos']['data'];
    for($j = 0; $j < count($photosdataarray); $j++){
        $output['photo'][]  =    array(
            'description'    =>    $photosdataarray[$j]['name'],
            'image_src'    =>    'source',
        );
    }
}

答案 2 :(得分:0)

考虑使用集合管道进行此类数据处理。你最终会得到更干净的代码(即没有foreach&gt; if&gt; foreach)。

您的代码示例(使用Knapsack):

$photos = Collection::from($raw_facebook['albums']['data'])
  ->extract('photos.data')
  ->flatten(1)
  ->filter(function (array $data) {
      return isset($data['name']) 
        && isset($data['source'])
        && $this->array_key_exists_recursive('source', $data);
  })
  ->map(function (array $data) {
      return [
        'description' => $data['name'],
        'image_src' => $data['source']
      ];
  })
  ->values()
  ->toArray();

库: