1

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.

1
  • you should take a look at array_map, it may be a lot easier than all those foreach loops. Commented May 30, 2016 at 19:16

3 Answers 3

1

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

    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

thanks a lot @Frayne. I modified your first answer (before you editted); $facebook['photo'] = array( 'description' => $photo['name'], 'image_src' => $photo['source'] ); to $facebook['photo'][] = array( 'description' => $photo['name'], 'image_src' => $photo['source'] ); and used it, as array_push gave me an error, for some reason.
Did you try after the edit?? What error array push generate?
php error: array_push() expects parameter 1 to be array, null given
so you need to make $facebook['photo'] = array() first.
sweet! initializing $facebook['photo'] as an empty array solved it. thanks!
|
1

Here you go

$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',
        );
    }
}

1 Comment

thanks Aditya. I bet this would work too. But I'm going with @Frayne, since this answer will involve changing my code too much.
0

Consider using collection pipeline for this kind of data processing. You'll end up with cleaner code (i.e. no foreach > if > foreach).

Example with your code (using 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();

Libraries:

3 Comments

ok, this looks really neat. but I'm almost at the end of the project (I don't want to implement anything new at this stage).. ..I'll maybe import the library later and try it out, for fun. thanks
how easily can the library be imported into Codeigniter? as that's the spec for the project
Well, just composer require it and you are good to go. Works on PHP 5.6 and up. No external dependencies so it should be just fine in a CI project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.