0

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
        )

)
1
  • 3
    You can use array_column(). Using the array_column() you will get an array with the product images and then use them where you want. OR you can loop through this array and echo the product images directly. Commented Oct 10, 2016 at 5:53

8 Answers 8

2

Try this,

foreach($array as $val)
{ 
    echo $val['product_image'];
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

guyz sorry ..but onnce again i have edited the question please go through
It only prints array..nothing else
1

You can take the array array_column and make it like this

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

Comments

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



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

    ?>

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
0
foreach($array as $key => $val){
  if($key == 'product_image'){
    echo "<img src='".$val."' />";
  }
}

Try

5 Comments

when i print $data['options']; the following result comes...So how should i use in above code
@priyankasawant why don't you go with Frayen comeent. You can get easily what you want. That are mentioned in other answers too.
use $data['options'] instead of $array
@khaliq its not working..Array ( [product_option_id] => 227 [product_id] => 54 [product_option_value] => Array ( ) [option_id] => 11 [name] => Size [type] => select [value] => [required] => 1 )
Place an array in a variable and then pass it into foreach loop.
0

You have to foreach the inner array:

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

2 Comments

what I can place in $array @igor unger
I assumed that you will use the array in your question. :)
0

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

9 Comments

@priyankasawant. Try this solution and share thoughts
this is printed Array ( [0] => Array ( ) )
By using which one solution one r two.
Try using array_column(). It will help you a lot .
Array ( [0] => Array ( ) )
|
0

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

Comments

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.