Laravel 5: Array Helper Methods: array_add

Helper Method: array_add


Standard Laravel definition:
The array_add function adds a given key / value pair to the array, if the given key doesn’t already exist in the array.

This method takes 3 arguments:

Lets explain above:

array: array we are adding to.

new_key: this is a key of a new array element we are adding to array.
This element cannot take an array.

new_value: this is a value of a new array element we are adding to array.
This element can be an array too.

example #1:

Result:

example #2:

Result: