Make WordPress Core


Ignore:
Timestamp:
10/14/2020 02:08:54 AM (4 years ago)
Author:
SergeyBiryukov
Message:

General: Move wp_array_get() from a separate file to wp-includes/functions.php, for consistency.

Add missing @since tag, adjust the DocBlock per the documentation standards.

Follow-up to [49135].

Props isabel_brison, ocean90.
Fixes #51461.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r49125 r49143  
    45284528
    45294529/**
     4530 * Accesses an array in depth based on a path of keys.
     4531 *
     4532 * It is the PHP equivalent of JavaScript's lodash.get, and mirroring it may help other components
     4533 * retain some symmetry between client and server implementations.
     4534 *
     4535 * @since 5.6.0
     4536 *
     4537 * @param array $array   An array from which we want to retrieve some information.
     4538 * @param array $path    An array of keys describing the path with which to retrieve information.
     4539 * @param array $default The return value if the path is not set on the array,
     4540 *                       or if the types of array and path are not arrays.
     4541 * @return array An array matching the path specified.
     4542 */
     4543function wp_array_get( $array, $path, $default = array() ) {
     4544    // Confirm input values are expected type to avoid notice warnings.
     4545    if ( ! is_array( $array ) || ! is_array( $path ) ) {
     4546        return $default;
     4547    }
     4548
     4549    $path_length = count( $path );
     4550
     4551    for ( $i = 0; $i < $path_length; ++$i ) {
     4552        if ( ! isset( $array[ $path[ $i ] ] ) ) {
     4553            return $default;
     4554        }
     4555        $array = $array[ $path[ $i ] ];
     4556    }
     4557
     4558    return $array;
     4559}
     4560
     4561/**
    45304562 * Filters a list of objects, based on a set of key => value arguments.
    45314563 *
Note: See TracChangeset for help on using the changeset viewer.