trying to learn trying to learn - 10 days ago 5
PHP Question

PHP loop through array inside array

I am trying to get url of low_resolution in this array using PHP and save results in new array.

array(3) {
["low_resolution"]=>
array(3) {
["url"]=>
string(181) "https://instagram.fskg1-1.fna.fbcdn.net/t51.2885-15/s320x320/e35/c180.0.720.720/15057412_1332184313482140_6642273356848562176_n.jpg?ig_cache_key=MTM5MDE1NzI5MDIyOTAwMjYwMQ%3D%3D.2.c"
["width"]=>
int(320)
["height"]=>
int(320)
}
["thumbnail"]=>
array(3) {
["url"]=>
string(181) "https://instagram.fskg1-1.fna.fbcdn.net/t51.2885-15/s150x150/e35/c180.0.720.720/15057412_1332184313482140_6642273356848562176_n.jpg?ig_cache_key=MTM5MDE1NzI5MDIyOTAwMjYwMQ%3D%3D.2.c"
["width"]=>
int(150)
["height"]=>
int(150)
}
["standard_resolution"]=>
array(3) {
["url"]=>
string(188) "https://instagram.fskg1-1.fna.fbcdn.net/t51.2885-15/s640x640/sh0.08/e35/c180.0.720.720/15057412_1332184313482140_6642273356848562176_n.jpg?ig_cache_key=MTM5MDE1NzI5MDIyOTAwMjYwMQ%3D%3D.2.c"
["width"]=>
int(640)
["height"]=>
int(640)
}
}
array(3) {
["low_resolution"]=>
array(3) {
["url"]=>
string(181) "https://instagram.fskg1-1.fna.fbcdn.net/t51.2885-15/s320x320/e35/c180.0.720.720/15043511_1031073267018441_5095610497747648512_n.jpg?ig_cache_key=MTM4Mjc1MTI3MDc2MjYyODE1Ng%3D%3D.2.c"
["width"]=>
int(320)
["height"]=>
int(320)
}
["thumbnail"]=>
array(3) {
["url"]=>
string(181) "https://instagram.fskg1-1.fna.fbcdn.net/t51.2885-15/s150x150/e35/c180.0.720.720/15043511_1031073267018441_5095610497747648512_n.jpg?ig_cache_key=MTM4Mjc1MTI3MDc2MjYyODE1Ng%3D%3D.2.c"
["width"]=>
int(150)
["height"]=>
int(150)
}
["standard_resolution"]=>
array(3) {
["url"]=>
string(188) "https://instagram.fskg1-1.fna.fbcdn.net/t51.2885-15/s640x640/sh0.08/e35/c180.0.720.720/15043511_1031073267018441_5095610497747648512_n.jpg?ig_cache_key=MTM4Mjc1MTI3MDc2MjYyODE1Ng%3D%3D.2.c"
["width"]=>
int(640)
["height"]=>
int(640)
}
}
array(3) {
["low_resolution"]=>
array(3) {
["url"]=>
string(164) "https://instagram.fskg1-1.fna.fbcdn.net/t51.2885-15/s320x320/e35/14474415_1652350801744421_4326205623935959040_n.jpg?ig_cache_key=MTM1MjkxNTUwNDI2ODEwMDc2NA%3D%3D.2"
["width"]=>
int(320)
["height"]=>
int(320)
}
["thumbnail"]=>
array(3) {
["url"]=>
string(164) "https://instagram.fskg1-1.fna.fbcdn.net/t51.2885-15/s150x150/e35/14474415_1652350801744421_4326205623935959040_n.jpg?ig_cache_key=MTM1MjkxNTUwNDI2ODEwMDc2NA%3D%3D.2"
["width"]=>
int(150)
["height"]=>
int(150)
}
["standard_resolution"]=>
array(3) {
["url"]=>
string(171) "https://instagram.fskg1-1.fna.fbcdn.net/t51.2885-15/s640x640/sh0.08/e35/14474415_1652350801744421_4326205623935959040_n.jpg?ig_cache_key=MTM1MjkxNTUwNDI2ODEwMDc2NA%3D%3D.2"
["width"]=>
int(640)
["height"]=>
int(640)
}
}


I tried using loops and foreach and cannot get the desired result . This is my code so far

for($i=0; $i < count($images['items']); $i++){
$data = $images['items'][$i]['images'];
if (is_array($data) || is_object($data))
{
var_dump($data);
}
}


$images variable contains all the data that has this structure:

array(3) {
["status"]=>
string(2) "ok"
["items"]=>
array(20) {
[0]=>
array(14) {
["can_delete_comments"]=>
bool(false)
["code"]=>
string(11) "BNK1LCqBPFp"
["location"]=>
NULL
["images"]=>
array(3) {
["low_resolution"]=>
array(3) {
["url"]=>
string(181) "https://instagram.fskg1-1.fna.fbcdn.net/t51.2885-15/s320x320/e35/c180.0.720.720/15057412_1332184313482140_6642273356848562176_n.jpg?ig_cache_key=MTM5MDE1NzI5MDIyOTAwMjYwMQ%3D%3D.2.c"
["width"]=>
int(320)
["height"]=>
int(320)
}
["thumbnail"]=>
array(3) {
["url"]=>
string(181) "https://instagram.fskg1-1.fna.fbcdn.net/t51.2885-15/s150x150/e35/c180.0.720.720/15057412_1332184313482140_6642273356848562176_n.jpg?ig_cache_key=MTM5MDE1NzI5MDIyOTAwMjYwMQ%3D%3D.2.c"
["width"]=>
int(150)
["height"]=>
int(150)
}
["standard_resolution"]=>
array(3) {
["url"]=>
string(188) "https://instagram.fskg1-1.fna.fbcdn.net/t51.2885-15/s640x640/sh0.08/e35/c180.0.720.720/15057412_1332184313482140_6642273356848562176_n.jpg?ig_cache_key=MTM5MDE1NzI5MDIyOTAwMjYwMQ%3D%3D.2.c"
["width"]=>
int(640)
["height"]=>
int(640)
}
}

Answer

If I understand your array structure correctly, you probably need this:

if (is_array($images))
foreach ($images["items"] as $item)
{
   if (is_array($item))
   foreach ($item as $img)
        {
            if (isset($img["low_resolution"]) && is_array($img["low_resolution"]))
            {
                echo $img["low_resolution"]["url"];
            }

        }
}