_wp_array_set

The timeline below displays how wordpress function _wp_array_set has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.4

/**
 * Sets an array in depth based on a path of keys.
 *
 * It is the PHP equivalent of JavaScript's `lodash.set()` and mirroring it may help other components
 * retain some symmetry between client and server implementations.
 *
 * Example usage:
 *
 *     $input_array = array();
 *     _wp_array_set( $input_array, array( 'a', 'b', 'c', 1 ) );
 *
 *     $input_array becomes:
 *     array(
 *         'a' => array(
 *             'b' => array(
 *                 'c' => 1,
 *             ),
 *         ),
 *     );
 *
 * @internal
 *
 * @since 5.8.0
 * @access private
 *
 * @param array $input_array An array that we want to mutate to include a specific value in a path.
 * @param array $path        An array of keys describing the path that we want to mutate.
 * @param mixed $value       The value that will be set.
 */
function _wp_array_set(&$input_array, $path, $value = null)
{
    // Confirm $input_array is valid.
    if (!is_array($input_array)) {
        return;
    }
    // Confirm $path is valid.
    if (!is_array($path)) {
        return;
    }
    $path_length = count($path);
    if (0 === $path_length) {
        return;
    }
    foreach ($path as $path_element) {
        if (!is_string($path_element) && !is_integer($path_element) && !is_null($path_element)) {
            return;
        }
    }
    for ($i = 0; $i < $path_length - 1; ++$i) {
        $path_element = $path[$i];
        if (!array_key_exists($path_element, $input_array) || !is_array($input_array[$path_element])) {
            $input_array[$path_element] = array();
        }
        $input_array =& $input_array[$path_element];
    }
    $input_array[$path[$i]] = $value;
}

WordPress Version: 6.2

/**
 * Sets an array in depth based on a path of keys.
 *
 * It is the PHP equivalent of JavaScript's `lodash.set()` and mirroring it may help other components
 * retain some symmetry between client and server implementations.
 *
 * Example usage:
 *
 *     $input_array = array();
 *     _wp_array_set( $input_array, array( 'a', 'b', 'c', 1 ) );
 *
 *     $input_array becomes:
 *     array(
 *         'a' => array(
 *             'b' => array(
 *                 'c' => 1,
 *             ),
 *         ),
 *     );
 *
 * @internal
 *
 * @since 5.8.0
 * @access private
 *
 * @param array $input_array An array that we want to mutate to include a specific value in a path.
 * @param array $path        An array of keys describing the path that we want to mutate.
 * @param mixed $value       The value that will be set.
 */
function _wp_array_set(&$input_array, $path, $value = null)
{
    // Confirm $input_array is valid.
    if (!is_array($input_array)) {
        return;
    }
    // Confirm $path is valid.
    if (!is_array($path)) {
        return;
    }
    $path_length = count($path);
    if (0 === $path_length) {
        return;
    }
    foreach ($path as $path_element) {
        if (!is_string($path_element) && !is_integer($path_element) && !is_null($path_element)) {
            return;
        }
    }
    for ($i = 0; $i < $path_length - 1; ++$i) {
        $path_element = $path[$i];
        if (!array_key_exists($path_element, $input_array) || !is_array($input_array[$path_element])) {
            $input_array[$path_element] = array();
        }
        $input_array =& $input_array[$path_element];
        // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration
    }
    $input_array[$path[$i]] = $value;
}

WordPress Version: 5.8

/**
 * Sets an array in depth based on a path of keys.
 *
 * It is the PHP equivalent of JavaScript's `lodash.set()` and mirroring it may help other components
 * retain some symmetry between client and server implementations.
 *
 * Example usage:
 *
 *     $array = array();
 *     _wp_array_set( $array, array( 'a', 'b', 'c', 1 ) );
 *
 *     $array becomes:
 *     array(
 *         'a' => array(
 *             'b' => array(
 *                 'c' => 1,
 *             ),
 *         ),
 *     );
 *
 * @internal
 *
 * @since 5.8.0
 * @access private
 *
 * @param array $array An array that we want to mutate to include a specific value in a path.
 * @param array $path  An array of keys describing the path that we want to mutate.
 * @param mixed $value The value that will be set.
 */
function _wp_array_set(&$array, $path, $value = null)
{
    // Confirm $array is valid.
    if (!is_array($array)) {
        return;
    }
    // Confirm $path is valid.
    if (!is_array($path)) {
        return;
    }
    $path_length = count($path);
    if (0 === $path_length) {
        return;
    }
    foreach ($path as $path_element) {
        if (!is_string($path_element) && !is_integer($path_element) && !is_null($path_element)) {
            return;
        }
    }
    for ($i = 0; $i < $path_length - 1; ++$i) {
        $path_element = $path[$i];
        if (!array_key_exists($path_element, $array) || !is_array($array[$path_element])) {
            $array[$path_element] = array();
        }
        $array =& $array[$path_element];
        // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration
    }
    $array[$path[$i]] = $value;
}