Suppose, I have an array
$my_array = array(
array(
array('cnt' => 1, 'date' => '2016-06-18', 'name' => 'Phone Calls'),
array('cnt' => 1,'date' => '2016-06-18','name' => 'Others Works')
),
array(
array('cnt' => 2 , 'date' => '2016-06-17', 'name' => 'Phone Calls'),
array('cnt' => 1, 'date' => '2016-06-17', 'name' => 'Others Works'),
array('cnt' => 1, 'date' => '2016-06-17', 'name' => 'Deal with Customer')
),
array(
array('cnt' => 4 , 'date' => '2016-06-15', 'name' => 'Phone Calls'),
array('cnt' => 5, 'date' => '2016-06-15', 'name' => 'Others Works'),
array('cnt' => 6, 'date' => '2016-06-15', 'name' => 'Deal with Customer')
)
.....
);
which I need to convert into
$desired_result = array(
array('date' => '2016-06-18',
'Phone Calls' => 1,//value is cnt of my_array
'Others Works' => 1,//value is cnt of my_array
),
array('date' => '2016-06-17',
'Phone Calls' => 2,//value is cnt of my_array
'Others Works' => 1,//value is cnt of my_array
'Deal with Customer' => 1,
),
array('date' => '2016-06-15',
'Phone Calls' => 4,//value is cnt of my_array
'Others Works' => 5,//value is cnt of my_array
'Deal with Customer' => 6,
)
.......
);
which I need to group by array with date with same array name as key and cnt as value.Can anyone suggest me how can I format this array.
I want to try http://bl.ocks.org/katirg/5f168b5c884b1f9c36a5 .
I am new to d3js, so I think better I will format my array.
You will need to loop the outer array and then each of those arrays. Take the index name and create an index in a new array that has a value of that arrays cnt.
$my_array = array(
array(
array('cnt' => 1, 'date' => '2016-06-18', 'name' => 'Phone Calls'),
array('cnt' => 1,'date' => '2016-06-18','name' => 'Others Works')
),
array(
array('cnt' => 2 , 'date' => '2016-06-17', 'name' => 'Phone Calls'),
array('cnt' => 1, 'date' => '2016-06-17', 'name' => 'Others Works'),
array('cnt' => 1, 'date' => '2016-06-17', 'name' => 'Deal with Customer')
),
array(
array('cnt' => 4 , 'date' => '2016-06-15', 'name' => 'Phone Calls'),
array('cnt' => 5, 'date' => '2016-06-15', 'name' => 'Others Works'),
array('cnt' => 6, 'date' => '2016-06-15', 'name' => 'Deal with Customer')
)
);
$betterFormat = array();
foreach($my_array as $arrays){
$newArray = array();
//Assume the date is the same in both arrays
$newArray['date'] = $arrays[0]['date'];
foreach($arrays as $arr){
$newArray[$arr['name']] = $arr['cnt'];
}
$betterFormat[] = $newArray;
}
echo "<pre>";
var_dump($betterFormat);
The output is
array(3) {
[0]=> array(3) {
["date"]=> string(10) "2016-06-18"
["Phone Calls"]=> int(1)
["Others Works"]=> int(1)
}
[1]=> array(4) {
["date"]=> string(10) "2016-06-17"
["Phone Calls"]=> int(2)
["Others Works"]=>int(1)
["Deal with Customer"]=> int(1)
}
[2]=> array(4) {
["date"]=> string(10) "2016-06-15"
["Phone Calls"]=> int(4)
["Others Works"]=> int(5)
["Deal with Customer"]=> int(6)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.