FreshPro
FreshPro

Reputation: 873

Php multidimensional array getting values

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?

Upvotes: 1

Views: 31190

Answers (4)

Bolebor
Bolebor

Reputation: 88

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

eg.:

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

Upvotes: 2

Karl
Karl

Reputation: 5463

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
}

Upvotes: 1

Guillaume Lhz
Guillaume Lhz

Reputation: 904

PHP :

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

Upvotes: 5

zavg
zavg

Reputation: 11061

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

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

Upvotes: 3

Related Questions