Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

306
Views
laravel - Can I add new items to an array that already has some existing items?

var_export($response) is an array like below:

array (
   0 =>
    array (
    'courseId' => 14,
    'tutorName' => 'admin',
    ),
   1 =>
    array (
    'courseId' => 15,
    'tutorName' => 'merl',
    ),
 )

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?

$dat=array_push($response,array('points'=>"3"));
return response()->json(['data' => $dat], 200);

Expected output:

[
    {
        "courseId": 14,
        "tutorName": "admin",
        "points": 3
    },
    {
        "courseId": 15,
        "tutorName": "merl",
        "points": 3
    }
]
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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:

foreach ($response as $key => $value) {
    $response[$key]['points'] = 3;
}

Then, just return the response :

return response()->json($response, 200);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!