php PHP多维数组获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19299137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Php multidimensional array getting values
提问by FreshPro
I have the following code sample
我有以下代码示例
private $analyze_types = array(
"1" => array(
'level' => '4',
'l1' => '-1',
'l2' => '-1',
'l3' => '-1',
'l4' => '-1',
'l5' => '-1'
),
"226" => array(
'level' => '-1',
'l1' => '-1',
'l2' => '-1',
'l3' => '2',
'l4' => '3',
'l5' => '4'
)
);
How can i get value of "1" and if I want to get 'level' value, what should i do?
我怎样才能获得“1”的价值,如果我想获得“等级”的价值,我该怎么办?
回答by Guillaume Lehezee
PHP :
PHP :
foreach( $this->analyze_types as $key => $value) {
echo $key; // output 1 and 226
echo $value['level']; // output 4 and -1
}
回答by zavg
To get element with index 'level'
of subarray with index '1'
in main array you should use just
要获取具有主数组索引'level'
的子'1'
数组索引的元素,您应该只使用
$this->analyze_types[1]['level']
回答by Bolebor
You can try array_column (http://php.net/manual/en/function.array-column.php)
你可以试试 array_column ( http://php.net/manual/en/function.array-column.php)
eg.:
例如。:
$levels = array_column($this->analyze_types, 'level');
回答by Karl
You can get the keys of an array by doing the following, if that's what you're asking?
您可以通过执行以下操作来获取数组的键,如果这就是您的要求?
$keys = array_keys($this->analyze_types);
print_r($keys);
Now that you have an array of keys you can simply loop through them to execute more code, for example:
现在您有了一组键,您可以简单地遍历它们以执行更多代码,例如:
foreach($keys as $k) {
echo $k; //This will echo out 1
}