Skip to content
Advertisement

laravel – Can I add new items to an array that already has some existing items?

var_export($response) is an array like below:

JavaScript

The below code gives a result like this: "data": 3. I wanted add a new item called points with the $response array, into all elements. But here, it overwrites the existing array. How can I achieve this?

JavaScript

Expected output:

JavaScript

Advertisement

Answer

As mentioned, array_push() returns the new number of elements in the array. That’s why you get 3.

You can add your value in all elements of the current response, like this:

JavaScript

Then, just return the response :

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement