Oihana PHP

getFirstValue.php

Table of Contents

Functions

getFirstValue()  : mixed
Returns the value of the first key from the provided list of keys that exists in the given array.

Functions

getFirstValue()

Returns the value of the first key from the provided list of keys that exists in the given array.

getFirstValue(array<string|int, mixed> $array, array<string|int, mixed> $keys[, mixed $default = null ]) : mixed

If none of the keys exist, the default value is returned.

Parameters
$array : array<string|int, mixed>

The array to search within.

$keys : array<string|int, mixed>

An ordered list of keys to check in the array.

$default : mixed = null

The value to return if none of the keys exist in the array. Default is null.

Tags
example
use function oihana\core\arrays\getFirstValue;

$definition = [
    'list' => [1, 2, 3],
    'concat' => 'abc',
];

$value = getFirstValue($definition, ['array', 'list', 'concat'], 'default');
// $value === [1, 2, 3]
author

Marc Alcaraz

since
1.0.0
Return values
mixed

Returns the value corresponding to the first matching key if found, otherwise returns the default value.


        
On this page

Search results