如何在php中从多维数组中打印数组特定值

时间:2016-10-10 05:51:17

标签: php arrays

下面是我打印的数组: -

我只想要循环中数组的product_image

 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 个答案:

答案 0 :(得分:2)

试试这个,

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

答案 1 :(得分:1)

您可以使用数组array_column并使其像这样

$(e.currentTarget).find('input[name="editId"]').val(editId);

答案 2 :(得分:1)

您编辑的输入的解决方案: -

$image_array = array();

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

输出: - https://eval.in/657966

答案 3 :(得分:1)

SELECT {
    padding-bottom: 6px;
} 

答案 4 :(得分:0)

解决方案一:

<?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().
?>

示例:

以下是您可以使用array_column()获取的PHP代码和示例输出。

<强> 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');

<强>输出:

如果我们在print_r()上致电$lastNames,您会看到一个看起来有点像这样的结果数组:

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

解决方案二:(根据要求最后

您可以单独在foreach中迭代单个键值,以便获得所需的参数。

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

答案 5 :(得分:0)

<?php echo "<script> document.getElementById('id').innerHTML = '$myresult' </script>"; ?>

尝试

答案 6 :(得分:0)

你必须预先处理内部数组:

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

答案 7 :(得分:0)

如果你想要一个指定的键并且想要对阵列结构做出最小的假设,请使用array_walk_recursive这样的

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