ap_set_in_array( array $arr, string $path, mixed $val, bool $merge_arr = false )

Description #

Set key => value in an array.

Parameters #

  • $arr
    array (Required) Array in which key value need to set.
  • $path
    string (Required) Path of new array item.
  • $val
    mixed (Required) Value to set.
  • $merge_arr
    bool (Optional) Should merge array. Default value: false

Changelog #

VersionDescription
1.0.0Introduced.

Source #

File: includes/functions.php

function ap_set_in_array( &$arr, $path, $val, $merge_arr = false ) {
	$path = is_string( $path ) ? explode( '.', $path ) : $path;
	$loc  = &$arr;

	foreach ( (array) $path as $step ) {
		$loc = &$loc[ $step ];
	}

	if ( $merge_arr && is_array( $loc ) ) {
		$loc = array_merge( $loc, $val );
	} else {
		$loc = $val;
	}

	return $loc;
}

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Add your comment