0

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

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

1 Answer 1

0

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);
Sign up to request clarification or add additional context in comments.

2 Comments

$position becomes 0 whick is not correct
@TraktorFieldwork Oops, forgot to set the 3rd parameter. See working edit

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.