217

I have an array as the following:

function example() {
    /* some stuff here that pushes items with
        dynamically created key strings into an array */

    return array( // now lets pretend it returns the created array
        'firstStringName' => $whatEver,
        'secondStringName' => $somethingElse
    );
}

$arr = example();

// now I know that $arr contains $arr['firstStringName'];

I need to find out the index of $arr['firstStringName'] so that I am able to loop through array_keys($arr) and return the key string 'firstStringName' by its index. How can I do that?

CharlesB
  • 86,532
  • 28
  • 194
  • 218
headacheCoder
  • 4,503
  • 8
  • 30
  • 33

9 Answers9

448

If you have a value and want to find the key, use array_search() like this:

$arr = array ('first' => 'a', 'second' => 'b', );
$key = array_search ('a', $arr);

$key will now contain the key for value 'a' (that is, 'first').

Yay295
  • 1,628
  • 3
  • 17
  • 29
zrvan
  • 7,533
  • 1
  • 22
  • 23
  • 29
    `array_filter()` can be used if you need to return all matching results instead of just the first matching one, as it preserves keys. – Mike Lyons Nov 27 '14 at 01:40
  • 6
    Something I think worth noting here: this solution was not working for me, until I discovered that sorting the array (sort($arr)) removes the key names, and resorts to the default 0,1,2,etc index values. So if you're sorting, use asort (asort($arr)). This maintains the key values. http://php.net/manual/en/function.asort.php – Rich701 Mar 09 '17 at 16:26
  • 1
    What if the values aren't unique? – Simon Roberts Aug 27 '22 at 21:34
  • @SimonRoberts Documentation solves your answer: returns the first corresponding key – netizen Apr 19 '23 at 08:10
83
key($arr);

will return the key value for the current array element

http://uk.php.net/manual/en/function.key.php

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • 1
    @AnthonyRutledge This will return only what the internal pointer is currently "pointed at". If we need another reference, then we should use zrvans method, which hunts down the requested value, specifically. – Parapluie Nov 21 '19 at 20:37
  • @Parapluie is right, I used this to my advantage, I only needed the first key which is the default position for the array pointer. You can use `reset($array)` to reset the pointer to the beginning if you need. – AutoBaker Nov 02 '22 at 13:13
55

If i understand correctly, can't you simply use:

foreach($arr as $key=>$value)
{
  echo $key;
}

See PHP manual

rwb
  • 4,309
  • 8
  • 36
  • 59
  • 1
    I think he is looking for array_search php function - http://php.net/manual/en/function.array-search.php – Gildonei Jul 17 '14 at 14:52
26

If the name's dynamic, then you must have something like

$arr[$key]

which'd mean that $key contains the value of the key.

You can use array_keys() to get ALL the keys of an array, e.g.

$arr = array('a' => 'b', 'c' => 'd')
$x = array_keys($arr);

would give you

$x = array(0 => 'a', 1 => 'c');
Marc B
  • 356,200
  • 43
  • 426
  • 500
25

Here is another option

$array = [1=>'one', 2=>'two', 3=>'there'];
$array = array_flip($array);
echo $array['one']; 
Demissew
  • 250
  • 4
  • 5
21

Yes you can infact php is one of the few languages who provide such support..

foreach($arr as $key=>$value)
{

}
Rajat Singhal
  • 11,234
  • 5
  • 38
  • 56
16

if you need to return an array elements with same value, use array_keys() function

$array = array('red' => 1, 'blue' => 1, 'green' => 2);
print_r(array_keys($array, 1));
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
aiswarya
  • 567
  • 6
  • 8
13

use array_keys() to get an array of all the unique keys.

Note that an array with named keys like your $arr can also be accessed with numeric indexes, like $arr[0].

http://php.net/manual/en/function.array-keys.php

Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
jan
  • 2,879
  • 2
  • 19
  • 28
8

you can use key function of php to get the key name:

<?php
    $array = array(
    'fruit1' => 'apple',
    'fruit2' => 'orange',
    'fruit3' => 'grape',
    'fruit4' => 'apple',
    'fruit5' => 'apple');

    // this cycle echoes all associative array
    // key where value equals "apple"
    while ($fruit_name = current($array)) {
      if ($fruit_name == 'apple') {
        echo key($array).'<br />';
      }
    next($array);
     }
?>

like here : PHP:key - Manual

rtroulak
  • 459
  • 1
  • 8
  • 16