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.

This question already has an answer here:

I can't understand how to get numeric index of "Index1" in such a structure:

$arr = array('Index','Index1' => array("one","two","three"),'Index2');

i'm trying to use array_search like so:

$index = array_search("Index1",$arr);

but it doesn't work; Thanks

share|improve this question

marked as duplicate by deceze Sep 18 '14 at 13:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2  
Index1 doesn't have a numeric index (see for yourself) –  kingkero Sep 18 '14 at 13:40
1  
The index is Index1. There's no "numeric index" for a string index. –  deceze Sep 18 '14 at 13:41
    
You could also write the array as array(0 => 'Index', 'Index1' => .., 1 => 'Index2') and produce the exact same structure –  kingkero Sep 18 '14 at 13:42
    
look at this stackoverflow.com/questions/3365766/… –  Jd patel Sep 18 '14 at 13:44
    
indeed, it doesn't exist @kingkero –  Traktor Fieldwork Sep 18 '14 at 13:51

1 Answer 1

up vote 0 down vote accepted

As mentioned, there is no numeric index for that element, as its key is a string.

However if you just want to find its position in the array:

$position = array_search("Index1",array_keys($arr),true);
share|improve this answer
    
$position becomes 0 whick is not correct –  Traktor Fieldwork Sep 18 '14 at 13:48
    
@TraktorFieldwork Oops, forgot to set the 3rd parameter. See working edit –  Steve Sep 18 '14 at 14:08
    
Thanks! thats exactly what i need! –  Traktor Fieldwork Sep 18 '14 at 14:28

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