Reputation: 1127
I have this array:
array (
0 =>
array (
'banner' => 'banner1',
'dayofweek' => '1',
'count' => '3',
),
1 =>
array (
'banner' => 'banner1',
'dayofweek' => '2',
'count' => '1',
),
2 =>
array (
'banner' => 'banner2',
'dayofweek' => '2',
'count' => '3',
)
)
I need to use it in highcharts to get two different vectors with the dayofweek as day and the count as value. This is how should be the highcharts array:
series:[
{
name:'banner1',
data:[3,1,0,0,0,0,0,]
},
{
name:'banner2',
data:[0,3,0,0,0,0,0,]
}
]
I need to have the array in this way I guess:
array (
0 =>
array (
'banner' => 'banner1',
'counts' => '3,1,0,0,0,0,0'
),
1 =>
array (
'banner' => 'banner2',
'counts' => '0,3,0,0,0,0,0'
)
)
How can I parse the array to get the format I need?
Upvotes: 0
Views: 68
Reputation: 1593
Actually what you need is something like this:
$initialArray = ...;
$temp = array();
foreach ($initialArray AS $a) {
if (!isset($temp[ $a['banner'] ])) {
$temp[ $a['banner'] ] = array_fill(0, 7, 0);
}
$temp[ $a['banner'] ][ $a['dayofweek'] - 1 ] = intval($a['count']);
}
$series = array();
foreach ($temp AS $banner => $counts) {
$obj = new stdClass();
$obj->banner = $banner;
$obj->counts = $counts;
$series[] = $obj;
}
echo json_encode($series);
It can be optimized to use less memory. But if you have not too much data (not millions of banners and hundreds of requests per second), then it is not needed.
Upvotes: 1
Reputation: 43479
If you mean something like php implode(), then try:
arrayName.join(delimiter);
Or simply loop array:
var str = '';
$.each(arrayName, function(i, num){
str += num + ',';
})
str.slice(-1);
Upvotes: 0