priyanka sawant
priyanka sawant

Reputation: 59

How to print a array specific value from multidimensional arrays in php

Below is my array which i have printed:-

I want only the product_image from the array in loop

 Array
(
    [0] => Array
        (
            [product_option_id] => 247
            [product_id] => 66
            [product_option_value] => Array
                (
                    [0] => Array
                        (
                            [product_option_value_id] => 42
                            [color_product_id] => 54
                            [name] => Pink
                            [product_image] => catalog/demo/teddy/03.jpg
                            [image] => http://192.168.15.9/Kids_stores/image/cache/catalog/axalta-ral-3015-light-pink-polyester-30-matt-powder-coating-20kg-box--1447-p-50x50.jpg
                            [price] => 
                            [price_prefix] => +
                        )

                    [1] => Array
                        (
                            [product_option_value_id] => 41
                            [color_product_id] => 67
                            [name] => Light Brown
                            [product_image] => catalog/Teddies/12-Baby-teddy/05.jpg
                            [image] => http://192.168.15.9/Kids_stores/image/cache/catalog/option-color/light_brown-50x50.jpg
                            [price] => 
                            [price_prefix] => +
                        )

                    [2] => Array
                        (
                            [product_option_value_id] => 43
                            [color_product_id] => 68
                            [name] => Cream 
                            [product_image] => catalog/Teddies/12-Baby-teddy/11.jpg
                            [image] => http://192.168.15.9/Kids_stores/image/cache/catalog/option-color/cream-images-50x50.jpg
                            [price] => 
                            [price_prefix] => +
                        )

                )

            [option_id] => 5
            [name] => COLOR
            [type] => image
            [value] => 
            [required] => 0
        )

)

Upvotes: 0

Views: 1755

Answers (8)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

Solution for your edited input:-

$image_array = array();

foreach ($your_array as $arr){
   $image_array[] = array_column($arr['product_option_value'],'product_image');
}

Output:- https://eval.in/657966

Upvotes: 1

cske
cske

Reputation: 2243

If you want one specified key and want minimal assumptions about array structure shoud use array_walk_recursive like this

 $result = []; 
 array_walk_recursive($input,function ($value,$key) use (&$result) {
           if ( 'product_image' == $key) {
               $result[] = $value;
           }
 });

Upvotes: 0

Naresh Kumar P
Naresh Kumar P

Reputation: 4210

Solution One:

<?php
$req_image=array();
$req_image[] = array_column($resultant_array, 'product_image');   
print_r($req_image); // This will print all the images that are grouped under the array().
?>

Example:

The below is the PHP code and the sample output that you can obtain using the array_column().

PHP:

<?php
$records = array(
    array(
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe'
    ),
    array(
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith'
    ),
    array(
        'id' => 5342,
        'first_name' => 'Jane',
        'last_name' => 'Jones'
    ),
    array(
        'id' => 5623,
        'first_name' => 'Peter',
        'last_name' => 'Doe'
    )
);

$lastNames = array_column($records, 'last_name', 'id');

Output:

If we call print_r() on $lastNames, you’ll see a resulting array that looks a bit like this:

Array
(
    [2135] => Doe
    [3245] => Smith
    [5342] => Jones
    [5623] => Doe
)

Solution Two: (As per requirement at last)

You can iterate the single key value alone in the foreach so that you can get the required parameter that you need.

foreach($resultant_array as $single_array)
{
  foreach($single_array['product_option_value'] as $inner_array)
  {
    echo $inner_array['product_image']; // This will print the u=mage name that you need.
  }
}

Upvotes: 0

priyanka sawant
priyanka sawant

Reputation: 59

    <?php $samples=$data['options'][0][product_option_value]; 



    $product_image = array_column($samples, 'product_image');
    echo'<pre>'; print_r($product_image );  

    ?>

Upvotes: 1

Igor Unger
Igor Unger

Reputation: 182

You have to foreach the inner array:

foreach($array[product_option_value] as $val)
{ 
    echo $val['product_image'];
}

Upvotes: 0

khaliq
khaliq

Reputation: 108

foreach($array as $key => $val){
  if($key == 'product_image'){
    echo "<img src='".$val."' />";
  }
}

Try

Upvotes: 0

S.I.
S.I.

Reputation: 3375

You can take the array array_column and make it like this

$records = array (
     array ( 
              // your array
           )
);
$variable = array_column($records, 'image');
echo $variable;

Upvotes: 1

Dave
Dave

Reputation: 3091

Try this,

foreach($array as $val)
{ 
    echo $val['product_image'];
}

Upvotes: 2

Related Questions