Issue
Is there any way to merge the array key with unique name, so i can get the unique key with their types and values. I am using yii2 and mysql
I am using the following SQL Query in yii2:
$data= $query->SELECT(["sum(no_of_pages) as pages_coded",'assigned_to','type_of_request','concat(firstname," ",lastname) as userfullname'])
->from('task')
->leftJoin('user','user.username = task.assigned_to')
->andWhere("sdlc_phase='Resolved'")
->orWhere("sdlc_phase='Semi_Resolved'");
$query->groupby(['assigned_to', 'type_of_request']);
//$query->->addGroupBy('assigned_to');
$query->orderby(['assigned_to'=>SORT_ASC]);
$data = $query->all();
and Output of SQL Query is
Array
(
[0] => Array
(
[pages_coded] => 5
[type_of_request] => Collector Tweak
[userfullname] => Sam
)
[1] => Array
(
[pages_coded] => 5
[type_of_request] => Collector Code
[userfullname] => John
)
[2] => Array
(
[pages_coded] => 42
[type_of_request] => Collector Tweak
[userfullname] => John
)
[3] => Array
(
[pages_coded] => 37
[type_of_request] => Clinical Tweak
[userfullname] => Dona
)
[4] => Array
(
[pages_coded] => 35
[type_of_request] => Collector Code
[userfullname] => Dona
)
[5] => Array
(
[pages_coded] => 7
[type_of_request] => Clinical Code
[userfullname] => Ricky
)
[6] => Array
(
[pages_coded] => 50
[type_of_request] => Clinical Tweak
[userfullname] => Ricky
)
[7] => Array
(
[pages_coded] => 4
[type_of_request] => Collector Code
[userfullname] => Ricky
)
[8] => Array
(
[pages_coded] => 17
[type_of_request] => Collector Tweak
[userfullname] => Ricky
)
)
However I want the generate the output like this, is there anyway to get this
Array
(
[Sam] => Array
(
[0] => Clinical Code
[1] => 0
[2] => Clinical Tweak
[3] => 0
[4] => Collector Code
[5] => 0
[6] => Collector Tweak
[7] => 5
)
[John] => Array
(
[0] => Clinical Code
[1] => 0
[2] => Clinical Tweak
[3] => 0
[4] => Collector Code
[5] => 5
[6] => Collector Tweak
[7] => 42
)
[Dona] => Array
(
[0] => Clinical Code
[1] => 0
[2] => Clinical Tweak
[3] => 37
[4] => Collector Code
[5] => 0
[6] => Collector Tweak
[7] => 35
)
[Ricky] => Array
(
[0] => Clinical Code
[1] => 7
[2] => Clinical Tweak
[3] => 50
[4] => Collector Code
[5] => 4
[6] => Collector Tweak
[7] => 17
)
)
help me sort out this issue.
Solution
For the output you want you will have to loop through the results creating another array.
$output = array();
foreach ($data as $row) {
$output[$row['userfullname']][$row['type_of_request']] = $row['pages_coded'];
}
Like this you will end up with arrays like:
$output = array(
'Sam' => array(
'Collector Tweak' => 5
),
);
And since you apparently want the other columns filled by 0 if they couldn't be found:
$categories = array(
'Clinical Code' => 0,
'Clinical Tweak' => 0,
'Collector Code' => 0,
'Collector Tweak' => 0,
);
foreach ($output as &$outputItem) {
$outputItem = array_merge($categories, $outputItem);
}
Which is, by definition, easier to track indexes and their respective values.
Tested here: https://3v4l.org/0k40Q
Answered By - Rafael
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.