WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/12/2020 08:18:08 PM (10 months ago)
Author:
johnbillion
Message:

General: Convert wp_array_get() to a "private" function and add tests.

This function may be promoted in the future if it's deemed useful enough.

Props dd32, jorgefilipecosta, Hareesh Pillai

Fixes #51720

File:
1 edited

Legend:

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

    r49552 r49580  
    45294529 * Accesses an array in depth based on a path of keys.
    45304530 *
    4531  * It is the PHP equivalent of JavaScript's lodash.get, and mirroring it may help other components
     4531 * It is the PHP equivalent of JavaScript's `lodash.get()` and mirroring it may help other components
    45324532 * retain some symmetry between client and server implementations.
    45334533 *
     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 *
    45344547 * @since 5.6.0
     4548 * @access private
    45354549 *
    45364550 * @param array $array   An array from which we want to retrieve some information.
    45374551 * @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 path are not arrays.
    4540  * @return array An array matching the 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 */
     4556function _wp_array_get( $array, $path, $default = null ) {
     4557    // Confirm $path is valid.
     4558    if ( ! is_array( $path ) || 0 === count( $path ) ) {
    45454559        return $default;
    45464560    }
    45474561
    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        ) {
    45524568            return $default;
    45534569        }
    4554         $array = $array[ $path[ $i ] ];
     4570        $array = $array[ $path_element ];
    45554571    }
    45564572
Note: See TracChangeset for help on using the changeset viewer.