简体   繁体   中英

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.

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

    }
}

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

Consider using collection pipeline for this kind of data processing. You'll end up with cleaner code (ie 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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM