Php 获取多维数组中元素的值
下面是我的foreach循环,用于从多维数组中获取值Php 获取多维数组中元素的值,php,arrays,Php,Arrays,下面是我的foreach循环,用于从多维数组中获取值 $_coloredvariables = get_post_meta( $post->ID, '_coloredvariables', true ); foreach ($_coloredvariables as $key => $value) { var_dump($value); } 哪个输出: array 'label' => string 'Color' (length=5) 'size' =>
$_coloredvariables = get_post_meta( $post->ID, '_coloredvariables', true );
foreach ($_coloredvariables as $key => $value) {
var_dump($value);
}
哪个输出:
array
'label' => string 'Color' (length=5)
'size' => string 'small' (length=5)
'displaytype' => string 'square' (length=6)
'values' =>
array
'dark-night-angel' =>
array
'type' => string 'Image' (length=5)
'color' => string '#2c4065' (length=7)
'image' => string '' (length=0)
'forest-green' =>
array
'type' => string 'Color' (length=5)
'color' => string '#285d5f' (length=7)
'image' => string '' (length=0)
'voilet' =>
array
'type' => string 'Color' (length=5)
'color' => string '#6539c9' (length=7)
'image' => string '' (length=0)
'canary-yellow' =>
array
'type' => string 'Color' (length=5)
'color' => string 'grey' (length=4)
'image' => string '' (length=0)
array
'dark-night-angel' =>
array
'type' => string 'Image' (length=5)
'color' => string '#2c4065' (length=7)
'image' => string '' (length=0)
'forest-green' =>
array
'type' => string 'Color' (length=5)
'color' => string '#285d5f' (length=7)
'image' => string '' (length=0)
'voilet' =>
array
'type' => string 'Color' (length=5)
'color' => string '#6539c9' (length=7)
'image' => string '' (length=0)
'canary-yellow' =>
array
'type' => string 'Color' (length=5)
'color' => string 'grey' (length=4)
'image' => string '' (length=0)
然后,为了仅获取值数组,我可以执行以下操作:
foreach ($_coloredvariables as $key => $value) {
var_dump($value['values']);
}
哪个输出:
array
'label' => string 'Color' (length=5)
'size' => string 'small' (length=5)
'displaytype' => string 'square' (length=6)
'values' =>
array
'dark-night-angel' =>
array
'type' => string 'Image' (length=5)
'color' => string '#2c4065' (length=7)
'image' => string '' (length=0)
'forest-green' =>
array
'type' => string 'Color' (length=5)
'color' => string '#285d5f' (length=7)
'image' => string '' (length=0)
'voilet' =>
array
'type' => string 'Color' (length=5)
'color' => string '#6539c9' (length=7)
'image' => string '' (length=0)
'canary-yellow' =>
array
'type' => string 'Color' (length=5)
'color' => string 'grey' (length=4)
'image' => string '' (length=0)
array
'dark-night-angel' =>
array
'type' => string 'Image' (length=5)
'color' => string '#2c4065' (length=7)
'image' => string '' (length=0)
'forest-green' =>
array
'type' => string 'Color' (length=5)
'color' => string '#285d5f' (length=7)
'image' => string '' (length=0)
'voilet' =>
array
'type' => string 'Color' (length=5)
'color' => string '#6539c9' (length=7)
'image' => string '' (length=0)
'canary-yellow' =>
array
'type' => string 'Color' (length=5)
'color' => string 'grey' (length=4)
'image' => string '' (length=0)
我不知道如何在数组结构中获取这些元素
“黑夜天使”,
“森林绿”,
“voilet”,
“金丝雀黄”
不使用特定名称:
var_dump($value['values']['dark-night-angel'])
一些更具活力的东西,当然这不起作用:
var_dump($value['values'][0][0]);
谢谢您可以在foreach循环中添加foreach循环
foreach($_coloredvariables as $key => $value) {
foreach($value['values'] as $valkey => $values) {
var_dump($valkey, $values);
}
}
对于1$value
(因此可能在该foreach中):
如果您想要
$\u coloredvariables
中所有值中的所有颜色,则需要该foreach:
$colors = array_keys($value['values']);
$colors = array();
foreach ($_coloredvariables as $key => $value) {
$colors = array_merge($colors, array_keys($value['values']));
}
这只返回颜色键。不是这些元素的内容(类型
,颜色
&图像
)
这就是您需要的吗?
$\u coloredvariables
只有一个元素?总是?$\u coloredvariables=get\u post\u meta($post->ID,'u coloredvariables',true)代码>我认为返回的post元数据不应该只有一个条目。您对每个var_转储进行处理,我在输出中只看到1个转储的var,所以我假设只有1个。。。你能显示一个var\u导出($\u coloredvariables)
吗,这样我们就知道它是什么样子了?我也试过了,很抱歉忘了提到,但它只得到了数组的内容:array'type'=>string'Color'(length=5)'Color'=>string'grey'(length=4)'image'=>string'(length=0)
不是该数组分组的名称'canary-yellow'=>
将$values)
替换为$valkey=>$values)
并且您可以使用$valkey获取密钥。这是一个很好的答案,为什么有人投票反对您?谁知道呢!?大家,这群混蛋!