_wp_array_set( array $input_array, array $path, mixed $value = null )
- Since
- 5.8.0
- Source
wp-includes/functions.php:5166
Description
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,
),
),
);Compatibility
- WordPress
- since 5.8.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$input_arrayarray- An array that we want to mutate to include a specific value in a path.
$patharray- An array of keys describing the path that we want to mutate.
$valuemixedoptional- The value that will be set.Default:
null
Performance profile
How much work a call to _wp_array_set() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Scales with input
- Instructions
- 6–22
- Plugin surface
- None
- Called by
- 21
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 45.
Nothing here hands control to plugin code.
21 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost _wp_array_set() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6–22 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 47 | 6–22 | 11 | 2 more instructions than PHP 8.5 |
| 8.5 | 45 | 6–22 | 11 | |
| 8.4 | 45 | 6–22 | 11 | |
| 8.3 | 45 | 6–22 | 11 | |
| 8.2 | 45 | 6–22 | 11 | |
| 8.1 | 45 | 6–22 | 11 | |
| 7.4 | 45 | 6–22 | 11 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Used by · 21
- WP_Duotone::migrate_experimental_duotone_support_flag()Migrates the experimental duotone support flag to the stabilized location.
- WP_Theme_JSON::__construct()Constructor.
- WP_Theme_JSON::do_opt_in_into_settings()Enables some settings.
- WP_Theme_JSON::get_data()Returns a valid theme.json as provided by a theme.
- WP_Theme_JSON::get_default_slugs()Returns the default slugs for all the presets in an associative array whose keys are the preset paths and the leaves is the list of slugs.
- WP_Theme_JSON::merge()Merges new incoming data.
- WP_Theme_JSON::preserve_valid_typed_settings()Preserves valid typed settings from input to output based on type markers in schema.
- WP_Theme_JSON::remove_indirect_properties()Removes indirect properties from the given input node and sets in the given output node.
- WP_Theme_JSON::remove_insecure_properties()Removes insecure data from theme.json.
- WP_Theme_JSON::remove_insecure_settings()Processes a setting node and returns the same node without the insecure settings.
- WP_Theme_JSON::remove_insecure_styles()Processes a style node and returns the same node without the insecure styles.
- WP_Theme_JSON::set_spacing_sizes()Sets the spacingSizes array based on the spacingScale values from theme.json.
Show all 21
- WP_Theme_JSON::unwrap_shared_block_style_variations()Unwraps shared block style variations.
- WP_Theme_JSON_Resolver::inject_variations_from_block_style_variation_files()Adds variations sourced from block style variations files to the supplied theme.json data.
- WP_Theme_JSON_Resolver::inject_variations_from_block_styles_registry()Adds variations sourced from the block styles registry to the supplied theme.json data.
- WP_Theme_JSON_Resolver::resolve_theme_file_uris()Resolves relative paths in theme.json styles to theme absolute paths and merges them with incoming theme JSON.
- WP_Theme_JSON_Schema::rename_settings()Processes a settings array, renaming or moving properties.
- wp_migrate_old_typography_shape()Converts typography keys declared under `supports.*` to `supports.typography.*`.
- wp_render_block_style_variation_support_styles()Renders the block style variation's styles.
- wp_render_custom_css_support_styles()Render the custom CSS stylesheet and add class name to block as required.
- wp_render_elements_support_styles()Render the elements stylesheet and adds elements class name to block as required.
Source code
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;}Changelog
Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/functions.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.