douxun4924
2016-10-10 05:51
浏览 50
已采纳

如何在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
        )

)
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

8条回答 默认 最新

  • doufei8691 2016-10-10 05:56
    已采纳

    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

    已采纳该答案
    打赏 评论
  • dongtao9095 2016-10-10 05:55

    Try this,

    foreach($array as $val)
    { 
        echo $val['product_image'];
    }
    
    打赏 评论
  • dpjuppr1361 2016-10-10 05:56

    You can take the array array_column and make it like this

    $records = array (
         array ( 
                  // your array
               )
    );
    $variable = array_column($records, 'image');
    echo $variable;
    
    打赏 评论
  • dqfkd82886 2016-10-10 06:01

    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.
      }
    }
    
    打赏 评论
  • doswy02440 2016-10-10 06:02
    foreach($array as $key => $val){
      if($key == 'product_image'){
        echo "<img src='".$val."' />";
      }
    }
    

    Try

    打赏 评论
  • duaiwu8385 2016-10-10 06:38

    You have to foreach the inner array:

    foreach($array[product_option_value] as $val)
    { 
        echo $val['product_image'];
    }
    
    打赏 评论
  • doujiang1832 2016-10-10 06:42
        <?php $samples=$data['options'][0][product_option_value]; 
    
    
    
        $product_image = array_column($samples, 'product_image');
        echo'<pre>'; print_r($product_image );  
    
        ?>
    
    打赏 评论
  • dongsu4345 2016-10-10 07:03

    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;
               }
     });
    
    打赏 评论

相关推荐 更多相似问题