<?php
$array = [
0 => [
"user_type" => 'Manager',
"total_count" => 4,
],
1 => [
"user_type" => 'Trainee',
"total_count" => 3,
]
];
$array_column = array_column($array, 'total_count', 'user_type');
//Create new array like below
$add_array = array('Manager'=>'0', 'Director'=>'0', 'Trainee'=>'0',);
//using array_merge merge add_array and array_column
$new_array = array_merge($add_array, $array_column);
//using array_walk_recursive get the requird result
array_walk_recursive($new_array, function($item, $key) use (&$final_array){
$final_array['labels'][]=$key;
$final_array['dataset'][]=$item;
});
echo "<pre>";
print_r($final_array);
?>
- Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- <pre>Array
(
[labels] => Array
(
[0] => Manager
[1] => Director
[2] => Trainee
)
[dataset] => Array
(
[0] => 4
[1] => 0
[2] => 3
)
)
preferences:
94.49 ms | 408 KiB | 5 Q