Changeset 49580 for trunk/src/wp-includes/functions.php
- Timestamp:
- 11/12/2020 08:18:08 PM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/functions.php
r49552 r49580 4529 4529 * Accesses an array in depth based on a path of keys. 4530 4530 * 4531 * It is the PHP equivalent of JavaScript's lodash.get,and mirroring it may help other components4531 * It is the PHP equivalent of JavaScript's `lodash.get()` and mirroring it may help other components 4532 4532 * retain some symmetry between client and server implementations. 4533 4533 * 4534 * Example usage: 4535 * 4536 * $array = array( 4537 * 'a' => array( 4538 * 'b' => array( 4539 * 'c' => 1, 4540 * ), 4541 * ), 4542 * ); 4543 * _wp_array_get( $array, array( 'a', 'b', 'c' ); 4544 * 4545 * @internal 4546 * 4534 4547 * @since 5.6.0 4548 * @access private 4535 4549 * 4536 4550 * @param array $array An array from which we want to retrieve some information. 4537 4551 * @param array $path An array of keys describing the path with which to retrieve information. 4538 * @param array $default The return value if the path is not set on the array,4539 * or if the types of array and pathare not arrays.4540 * @return array An array matchingthe path specified.4541 */ 4542 function wp_array_get( $array, $path, $default = array()) {4543 // Confirm input values are expected type to avoid notice warnings.4544 if ( ! is_array( $ array ) || ! is_array( $path ) ) {4552 * @param mixed $default The return value if the path does not exist within the array, 4553 * or if `$array` or `$path` are not arrays. 4554 * @return mixed The value from the path specified. 4555 */ 4556 function _wp_array_get( $array, $path, $default = null ) { 4557 // Confirm $path is valid. 4558 if ( ! is_array( $path ) || 0 === count( $path ) ) { 4545 4559 return $default; 4546 4560 } 4547 4561 4548 $path_length = count( $path ); 4549 4550 for ( $i = 0; $i < $path_length; ++$i ) { 4551 if ( ! isset( $array[ $path[ $i ] ] ) ) { 4562 foreach ( $path as $path_element ) { 4563 if ( 4564 ! is_array( $array ) || 4565 ( ! is_string( $path_element ) && ! is_integer( $path_element ) && ! is_null( $path_element ) ) || 4566 ! array_key_exists( $path_element, $array ) 4567 ) { 4552 4568 return $default; 4553 4569 } 4554 $array = $array[ $path [ $i ]];4570 $array = $array[ $path_element ]; 4555 4571 } 4556 4572
Note: See TracChangeset
for help on using the changeset viewer.