Click here to Skip to main content
15,888,171 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an array like below

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


I need to create another array with this arrays total value group by examCourseId
PHP
For examCourseId = 1039 it need to be

[2.3, -3.0]

For examCourseId = 10007 it need to be

[15.5]


Total Array should be

[
  [2.3, -3.0],
  [15.5]
]


What I have tried:

$group = array();
 foreach (array as $value) {
   $group[$value['examCourseId']][] = $value;
}
Posted
Updated 10-Mar-22 20:05pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900