PHP循环遍历数组内部的数组

时间:2016-11-28 10:41:09

标签: php arrays

我正在尝试使用PHP在此数组中获取low_resolution的url并将结果保存在新数组中。

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

我尝试使用循环和foreach,但无法获得所需的结果。到目前为止,这是我的代码

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

$ images变量包含具有此结构的所有数据:

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 个答案:

答案 0 :(得分:2)

您的阵列结构是正确的。访问所需内容的最简单方法是:

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

为了额外的安全性,添加一个控件以避免由于没有图像的$ images而导致的错误 - &gt; low_resolution-&gt; 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 :(得分:1)

如果我理解你的数组结构,你可能需要这个:

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

        }
}

答案 2 :(得分:1)

我假设你有如下结构:

SDWebImage

现在您可以像这样过滤它:

$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 */
];

结果如下:

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

答案 3 :(得分:0)

试试这个

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