| 1 |
<?php |
| 2 |
/** |
| 3 |
* General utilities. |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* This function allows to easily access a part from a php array. |
| 10 |
* It is equivalent to want lodash get provides for JavaScript and is useful to have something similar |
| 11 |
* in php so functions that do the same thing on the client and sever can have identical code. |
| 12 |
* |
| 13 |
* @param array $array An array from where we want to retrieve some information from. |
| 14 |
* @param array $path An array containing the path we want to retrieve. |
| 15 |
* @param array $default The return value if $array or $path is not expected input type. |
| 16 |
* |
| 17 |
* @return array An array matching the path specified. |
| 18 |
*/ |
| 19 |
function gutenberg_experimental_get( $array, $path, $default = array() ) { |
| 20 |
// Confirm input values are expected type to avoid notice warnings. |
| 21 |
if ( ! is_array( $array ) || ! is_array( $path ) ) { |
| 22 |
return $default; |
| 23 |
} |
| 24 |
|
| 25 |
$path_length = count( $path ); |
| 26 |
for ( $i = 0; $i < $path_length; ++$i ) { |
| 27 |
if ( empty( $array[ $path[ $i ] ] ) ) { |
| 28 |
return $default; |
| 29 |
} |
| 30 |
$array = $array[ $path[ $i ] ]; |
| 31 |
} |
| 32 |
return $array; |
| 33 |
} |
| 34 |
|