Sam Sam - 6 months ago 18
PHP Question

php build simpler array from a multidimensional (json reponse) array

I am trying to get data from a multidimensional array I got from a Facebook response, into a simpler array.

the json data is as seen below.

{
"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="
}
},
}


I want to end up with an array like this:

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......
)
)


But I get this:

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...

)
)


This is the code for my current loop:

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'];
}
}

}
}


When I modify this code to this:

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++;

}
}


I get a result similar to what I need, but with only 6 inner arrays (when there should be like 10 of them), showing the iteration isn't going correctly.

Please I'd appreciate to be pointed in the right direction.

Answer

Did this: 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'] ));
            }
        }

    }
}
Comments