-
Notifications
You must be signed in to change notification settings - Fork 1
array_add
Tonya Mork edited this page Mar 12, 2017
·
3 revisions
The array_add() function adds the new element(s) you specify at the specified location (via key) when the element does not already exist. If successful, the new array is returned; else, the original array is returned.
Yes. The second parameter can be dot notation to indicate a deeply nested array position.
mixed array_add(
array $subjectArray,
string|array $newElementKey,
mixed $newElementValue
);
where,
$subjectArray is the array to work on
$newElementKey is the new element's key. For deeply nested arrays, you can use dot notation or an array of keys. When using an array, element 0 is first depth level, element 1 is the next level, etc: array( 'level1', 'level2' )
$newElementValue is the new element's value.
$user = array(
'user_id' => 504,
'name' => 'Bob Jones',
'social' => array(
'twitter' => '@bobjones',
),
);
$user = array_add( $user, 'social.website', 'https://bobjones.com' );
$user = array_add( $user, array( 'social', 'website' ), 'https://bobjones.com' );
/**
array(
'user_id' => 504,
'name' => 'Bob Jones',
'social' => array(
'twitter' => '@bobjones',
'website' => 'https://bobjones.com',
),
)
*/