Arr::add

Posted on November 14, 2016

The Arr::add function adds a given key / value pair to the array if the given key doesn’t already exist in the array.

Example usage

$array = Arr:add(['name' => 'Desk'], 'price', 100);

// ['name' => 'Desk', 'price' => 100]
$array = Arr:add(
    [
        'name' => 'John Doe',
        'age'  => 32,
    ],
    'age',
    '25'
);

// ['name' => 'John Doe', 'age' => 32]
$array = Arr:add(
    [
        'name' => 'John Doe',
        'age'  => 32,
    ],
    'kids.0',
    ['name' => 'Baby Doe', 'age' => 1]
);

// [
//    'name' => 'John Doe',
//    'age'  => 32,
//    'kids' => [
//        [
//            'name' => 'Baby Doe',
//            'age'  => 1
//        ]
//    ]
// ]

Parameters

array $array

string $key

mixed $value

Return Value

Array with new element.

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *