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

169
Views
¿Cómo puedo ordenar esta matriz?

Tengo una matriz como esta.

 array:3 [▼ 0 => array:2 [▼ "id" => "1039" "total" => 2.3 ] 1 => array:2 [▼ "id" => "1039" "total" => -3.0 ] 2 => array:2 [▼ "id" => "10007" "total" => 15.5 ] ]

Necesito "total" para el mismo "id" en la misma matriz. por ejemplo, para "id" = 1039, la matriz debe ser

 [2.3, -3.0]

Para "id" = 10007, la matriz debe ser

 [15.5]

Mi matriz deseada es

 [ [2.3, -3.0], [15.5] ]
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Puede recorrer su matriz y configurarla en otra matriz de esta manera:

 $array = [ ['id' => 1, 'value' => 2], ['id' => 1, 'value' => 3], ['id' => 2, 'value' => 5], ]; $temp = []; foreach($array as $element){ if(!isset($temp[$element['id']])){ $temp[$element['id']] = []; } $temp[$element['id']][] = $element['value']; } var_dump($temp);
over 4 years ago · Santiago Trujillo Report

0

Realmente quiere que estén codificados por la ID de las matrices.

 $arr = [ ["id" => "1039", "total" => 2.3],["id" => "1039","total" => -3.0],["id" => "10007","total" => 15.5]]; $total = []; foreach($arr as $item) { $total[$item['id']][] = $item['total']; }

eso regresará

 array:2 [ 1039 => array:2 [ 0 => 2.3 1 => -3.0 ] 10007 => array:1 [ 0 => 15.5 ] ]
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!