1

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

4 Answers 4

1

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

        }
}
4
  • I have tried something similar to this , but the low_resolution array contains width and height value , and I want to get the url of it Commented Nov 28, 2016 at 10:47
  • For some reason it throws an error Illegal string offset 'low_resolution' , Also removing ['low_resolution'] from if statement, echo line throws an error : Undefined index: low_resolution . I really do not know what is causing this. Commented Nov 28, 2016 at 10:56
  • This means you don't have the low_resolution in some of your array items. One second. Commented Nov 28, 2016 at 10:57
  • Thanks for your help , actually that was the problem . Commented Nov 28, 2016 at 11:43
1

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

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']; 
    }
}
1
  • That code works too, but you forgot to add ['items'] instead of ['items'] in foreach statement . Thank you for your answer ! Commented Nov 28, 2016 at 11:44
0

Try this

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

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.