[英]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 我只需要循环中数组中的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
)
)
Try this, 尝试这个,
foreach($array as $val)
{
echo $val['product_image'];
}
You can take the array array_column and make it like this 您可以将数组array_column设置为这样
$records = array (
array (
// your array
)
);
$variable = array_column($records, 'image');
echo $variable;
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 输出: -https : //eval.in/657966
<?php $samples=$data['options'][0][product_option_value];
$product_image = array_column($samples, 'product_image');
echo'<pre>'; print_r($product_image );
?>
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代码以及可以使用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');
Output: 输出:
If we call print_r()
on $lastNames
, you'll see a resulting array that looks a bit like this: 如果我们在$lastNames
上调用print_r()
,您将看到一个结果数组,看起来像这样:
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中单独迭代单个键值,以便获得所需的必需参数。
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.
}
}
foreach($array as $key => $val){
if($key == 'product_image'){
echo "<img src='".$val."' />";
}
}
Try 尝试
You have to foreach the inner array: 您必须先获取内部数组:
foreach($array[product_option_value] as $val)
{
echo $val['product_image'];
}
If you want one specified key and want minimal assumptions about array structure shoud use array_walk_recursive like this 如果您需要一个指定的键,并且希望对数组结构的假设最少,请使用array_walk_recursive这样的
$result = [];
array_walk_recursive($input,function ($value,$key) use (&$result) {
if ( 'product_image' == $key) {
$result[] = $value;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.