trying to learn
trying to learn

Reputation: 129

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

Upvotes: 1

Views: 83

Answers (4)

MarcM
MarcM

Reputation: 2251

Your array structure is right. The simplest way to access what you need is:

$your_new_array = array(); // create new empty array to store low_resolution images

foreach ($images[items] as $image_item)
{
    // for each $images item, add images->low_resolution->url value to $your_new_array
    $your_new_array[] = $image_item['images']['low_resolution']['url']; 
}

For extra security, add a control to avoid errors due to $images without images->low_resolution->url

$your_new_array = array(); // create new empty array to store low_resolution images

foreach ($images[items] as $image_item)
{
    // for each $images item, if exisit images->low_resolution->url...
    if (isset($image_item['images']['low_resolution']['url']))
    {
        // ...add images->low_resolution->url value to $your_new_array
        $your_new_array[] = $image_item['images']['low_resolution']['url']; 
    }
}

Upvotes: 1

Mikołaj Woźniak
Mikołaj Woźniak

Reputation: 113

I assumed you have the structure like below:

$data = [
    /*(..) Some structure */
    'items' =>
        [
            'images' => [
                [
                    'low_resolution' => [
                        'url' => "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' => 1234,
                        'height' => 122
                    ],
                    'thumbnail' => [
                        'url' => "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' => 1234,
                        'height' => 122
                    ]
                ],
                [
                    'low_resolution' => [
                        'url' => "", //some empty url
                        'width' => 1234,
                        'height' => 122
                    ],
                    'thumbnail' => [
                        'url' => "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' => 1234,
                        'height' => 122
                    ]
                ],

            ],

        ]
    /*(..) Some structure */
];

Now you can filter it like this:

// array filter skips empty urls or not existing properties
// array map iterates through the array of images and returns new one with urls only
$lowResolutionUrls = array_filter(array_map(function ($image) {

    return (isset($image['low_resolution']) && isset($image['low_resolution']['url']))
        ? $image['low_resolution']['url']
        : null;
}, $data['items']['images']), function($url){
    return !!$url;
});

the result is like below:

Array
(
    [0] => 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
)

Upvotes: 1

Phiter
Phiter

Reputation: 14977

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

        }
}

Upvotes: 1

Rahul
Rahul

Reputation: 2474

Try this

foreach ($images["items"] as $image)
{
   foreach ($image as $img)
        {
            echo $img["low_resolution"]["url];
        }
}

Upvotes: 0

Related Questions