-2

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

Aaranihlus
  • 143
  • 2
  • 13

3 Answers3

0

Try this,

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

Here is the link of array_column()

Rahul
  • 18,271
  • 7
  • 41
  • 60
  • Can i use that in a loop? I have tried `foreach($image_paths as $image){$image[0]}` but I still don't get anything – Aaranihlus Mar 06 '17 at 11:04
  • No need to use loop at all just below your array, Try once above code for your array, you will get what you need, trust me – Rahul Mar 06 '17 at 11:05
0
foreach($images as $image)
{
   echo $image['image_path'];
}

or

echo $images[0]['image_path'];
Frankich
  • 842
  • 9
  • 19
0

Try this

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

working example - http://codepad.org/i0B5X8lW

Mani7TAM
  • 469
  • 3
  • 10