1

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?

6
  • Don't think I understand your question. Can you be more detailed? Commented Oct 10, 2013 at 14:54
  • Well, I have a protected function in the same class with this array. And I want to get 1 and 226 in this case. Commented Oct 10, 2013 at 14:55
  • @FreshPro: Are you looking for array_keys or something similar? Commented Oct 10, 2013 at 14:56
  • what's the criteria to fetch the result when you say you want to get 1 and 126? Commented Oct 10, 2013 at 15:01
  • 1
    It sounds a rather basic question. Just as other people suggested, either foreach or array_keys will do the trick. I just wonder what effort you have made to solve this? Commented Oct 10, 2013 at 15:08

4 Answers 4

5

PHP :

foreach( $this->analyze_types as $key => $value) {
  echo $key; // output 1 and 226
  echo $value['level']; // output 4 and -1
}
Sign up to request clarification or add additional context in comments.

Comments

3

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

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

1 Comment

@FreshPro So array_keys($this->analyze_types) as other people suggested
2

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

eg.:

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

Comments

1

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.