Php multidimensional array getting values

匿名 (未验证) 提交于 2019-12-03 09:02:45

问题:

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:

PHP :

foreach( $this->analyze_types as $key => $value) {   echo $key; // output 1 and 226   echo $value['level']; // output 4 and -1 } 


回答2:

To get element with index 'level' of subarray with index '1' in main array you should use just

$this->analyze_types[1]['level'] 


回答3:

You can try array_column (http://php.net/manual/en/function.array-column.php)

eg.:

$levels = array_column($this->analyze_types, 'level'); 


回答4:

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 } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!