可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 }