2

Here is the vardump for my array:

array(4) {
         [3]=> array(1) 
         {
             ["match"]=> int(33) 
         } 
         [2]=> array(1) 
         { 
             ["match"]=> int(32) 
         } 
         [1]=> array(1) 
         { 
             ["match"]=> int(16) 
         } 
         [4]=> array(1) 
         { 
             ["match"]=> int(3) 
         } 
}

I need to return the indexes 3, 2, 1, and 4 for use in a query. I have no idea how to do this. I need to run the query in a foreach statement:

foreach($arrayName as $key){
    //NEED TO RETURN INDEX HERE    
}

I've tried to use key($key) but that returned the word "match" which is the index of one level below where I need.

Any help will be appreciated.

1
  • In fact you are using foreach with the wrong semantics. It sould be like foreach($arrayName as $value) if you are using two operands. Commented Mar 2, 2011 at 15:25

4 Answers 4

7
foreach($arrayName as $key => $value){
    echo($key);    
}
Sign up to request clarification or add additional context in comments.

Comments

6

Easy one ;)

$keys = array_keys($arrayName);

2 Comments

Avoid running queries in loops - you should be more than able to formulate one query to it for you by using something like IN('.implode(',',$keys).') ...
Well I thought it was going to be more complicated than that. This is my first foray into large arrays, cheers for the help
4
foreach($arrayName as $key)

The $key is actually the value in the array. Try:

foreach($arrayName as $key=>$value)

Comments

3

Just add the key to the foreach:

foreach($arrayName as $key => $value){
       echo $key; //$key is well... the key and $value is the value of the current element in the array :)
}

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.