PHP.nl

array_find_key

array_find_key

Returns the key of the first element satisfying a callback function

mixed **array_find_key** array $array callable $callback
returns the key of the first element of an

array for which the given returns true. If no matching element is found the function returns null. array_find_key``callback

arrayThe array that should be searched.

callback The callback function to call to check each element, which must be

  If this function returns true, the key is returned from
   and the callback will not be called
  for further elements.
 ```php

bool **** mixed $value mixed $key


`array_find_key`


   The function returns the key of the first element for which the
    returns true. If no matching element is
   found the function returns null.
  `callback`

**Voorbeeld:  example**

```php
<?php
$array = [
    'a' => 'dog',
    'b' => 'cat',
    'c' => 'cow',
    'd' => 'duck',
    'e' => 'goose',
    'f' => 'elephant'
];

// Find the first animal with a name longer than 4 characters.
var_dump(array_find_key($array, function (string $value) {
    return strlen($value) > 4;
}));

// Find the first animal whose name begins with f.
var_dump(array_find_key($array, function (string $value) {
    return str_starts_with($value, 'f');
}));

// Find the first animal where the array key is the first symbol of the animal.
var_dump(array_find_key($array, function (string $value, $key) {
   return $value[0] === $key;
}));

// Find the first animal where the array key matching a RegEx.
var_dump(array_find_key($array, function ($value, $key) {
   return preg_match('/^([a-f])$/', $key);
}));
?>
string(1) "e"
NULL
string(1) "c"
string(1) "a"

array_find``array_all``array_any``array_filter``array_reduce