Retrieve array value by index

时间:2017-03-06 10:45:56

标签: php

If I have an array like this:

Array
(
    [0] => Array
        (
            [image_path] => 3blocks-02.png
        )

    [1] => Array
        (
            [image_path] => 2blocks-02.png
        )
)

Is there a way I can retrieve the ['image_path'] value by the array index in a forreach loop?

I have tried:

foreach($images as $image)
{
   $image[1]
}

and 

foreach($images as $image)
{
   $image[1]['image_path']
}

and a key => value loop but I cant seem to get the data

3 个答案:

答案 0 :(得分:0)

试试这个,

$image_paths = array_column($your_array,'image_path');
print_r($image_paths);

以下是array_column()

的链接

答案 1 :(得分:0)

foreach($images as $image)
{
   echo $image['image_path'];
}

echo $images[0]['image_path'];

答案 2 :(得分:0)

试试这个

<?php
$array = Array(
          Array(image_path => '3blocks-02.png'),
          Array(image_path => '2blocks-02.png')
);


foreach ($array as $key => $value) {
  print_r($value['image_path']); 
}

工作示例 - http://codepad.org/i0B5X8lW