PHP - Get key name of array value

PhpArraysArray Key

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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').

Solution 2 - Php

key($arr);

will return the key value for the current array element

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

Solution 3 - Php

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

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

See PHP manual

Solution 4 - Php

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');

Solution 5 - Php

Here is another option

$array = [1=>'one', 2=>'two', 3=>'there'];
$array = array_flip($array);
echo $array['one']; 

Solution 6 - Php

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

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

}

Solution 7 - Php

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));

Solution 8 - Php

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

Solution 9 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionheadacheCoderView Question on Stackoverflow
Solution 1 - PhpzrvanView Answer on Stackoverflow
Solution 2 - PhpMark BakerView Answer on Stackoverflow
Solution 3 - PhprwbView Answer on Stackoverflow
Solution 4 - PhpMarc BView Answer on Stackoverflow
Solution 5 - PhpDemissewView Answer on Stackoverflow
Solution 6 - PhpRajat SinghalView Answer on Stackoverflow
Solution 7 - PhpaiswaryaView Answer on Stackoverflow
Solution 8 - PhpjanView Answer on Stackoverflow
Solution 9 - PhprtroulakView Answer on Stackoverflow