Search code examples
phparraysarray-key

How to get the position of a key within an array


Ok, so I need to grab the position of 'blah' within this array (position will not always be the same). For example:

$array = (
    'a' => $some_content,
    'b' => $more_content,
    'c' => array($content),
    'blah' => array($stuff),
    'd' => $info,
    'e' => $more_info,
);

So, I would like to be able to return the number of where the 'blah' key is located at within the array. In this scenario, it should return 3. How can I do this quickly? And without affecting the $array array at all.


Solution

  • $i = array_search('blah', array_keys($array));