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

313
Views
laravel - ¿Puedo agregar nuevos elementos a una matriz que ya tiene algunos elementos existentes?

var_export($response) es una matriz como la siguiente:

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

El siguiente código da un resultado como este: "data": 3 . Quería agregar un nuevo elemento llamado points con la matriz $response , en todos los elementos. Pero aquí, sobrescribe la matriz existente. ¿Cómo puedo conseguir esto?

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

Rendimiento esperado:

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

0

Como se mencionó, array_push() devuelve la nueva cantidad de elementos en la matriz. Es por eso que obtienes 3 .

Puede agregar su valor en todos los elementos de la respuesta actual, así:

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

Luego, solo devuelve la respuesta:

 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!