Search code examples
phparraysfetchexistsidentifier

Fetch/ se if exists, identifier in array


How can I grab the identifier in an array?

Like this example

Array
(
    [6] => Stil
    [8] => Färg
    [7] => Material
)

I want to fetch the 6, 8 and 7. To fetch "Stil", "Färg" and "Material" it is pretty easy. $arr[6] and so on. But if I want to get or see if the 6, 8 or 7 exists. How do I manage this?


Solution

  • Use array_search.

    array_search('Stil', $array); // 6
    

    Or if the value is the array multiple times, use array_keys.

    array_keys($array, 'Stil'); // array(0 => 6)
    

    UPDATE: If you don't know the value, you can use array_key_exists:

    array_key_exists(6, $array); // true