The PHP Arr get function is a simple utility function that returns the value of a specified key from an associative array. This function helps to simplify the code by avoiding the use of nested if statements to check if a key exists in an array.
For example, consider the following association array:
To get the age of John using nested if statements:
if (isset($users['John']) && isset($users['John']['age'])) { $johnAge = $users['John']['age']; } else { $johnAge = null; // or default value }
Using the PHP Arr get function:
$johnAge = Arr::get($users, 'John.age', null);
Here, the Arr class is assumed to be part of a package or library that provides this function. The function takes three parameters: the array to search, the key to retrieve (with nested keys separated by dots), and a default value to return if the key is not found.
This function can also be used with multidimensional arrays or objects.
For example, to get the email address of a user object:
$user = new stdClass(); $user->id = 123; $user->name = 'John Doe'; $user->contact = new stdClass(); $user->contact->email = 'johndoe@example.com';
$email = Arr::get($user, 'contact.email', '');
Here, the function returns the email address of the $user object's contact property, which is a nested object. If the email property was not found, an empty string is returned as the default value.
PHP Arr::get - 30 examples found. These are the top rated real world PHP examples of Arr::get extracted from open source projects. You can rate examples to help us improve the quality of examples.