Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
Don't think I understand your question. Can you be more detailed? –  aarryy Oct 10 '13 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. –  FreshPro Oct 10 '13 at 14:55
    
@FreshPro: Are you looking for array_keys or something similar? –  Jon Oct 10 '13 at 14:56
    
what's the criteria to fetch the result when you say you want to get 1 and 126? –  aarryy Oct 10 '13 at 15:01
    
Well I'll fetch results and I need 1 and 226 to compare if fld_type=1 or fld_type=226 exists in database and if not i will insert inner array values in database that's why i'm asking –  FreshPro Oct 10 '13 at 15:03

3 Answers 3

up vote 3 down vote accepted

PHP :

foreach( $this->analyze_types as $key => $value) {
  echo $key; // output 1 and 226
  echo $value['level']; // output 4 and -1
}
share|improve this answer
    
Thank you so much! –  FreshPro Oct 10 '13 at 15:06
    
No problem, we are here to help you :p –  Guillaume Lehezee Oct 10 '13 at 15:07

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

$this->analyze_types[1]['level']
share|improve this answer
    
Ok that gives me 4 thats good but how to get the "1" –  FreshPro Oct 10 '13 at 15:01
    
@FreshPro So array_keys($this->analyze_types) as other people suggested –  zavg Oct 10 '13 at 15:04

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
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.