How to sum multidimensional array values then grouping with date as my code. If any PHP code what should I try please tell me.
Please see the array code:
$array = array (
0 => array(
'date' => '2015-02-06 10:42:39',
'visit' => 1,
'bounce' => 0
),
1 => array(
'date' => '2015-02-06 13:23:21',
'visit' => 1,
'bounce' => 1
),
2 => array(
'date' => '2015-02-07 04:11:42',
'visit' => 1,
'bounce' => 1
),
3 => array(
'date' => '2015-02-08 11:35:28',
'visit' => 1,
'bounce' => 1
),
4 => array(
'date' => '2015-02-08 15:12:09',
'visit' => 1,
'bounce' => 1
),
5 => array(
'date' => '2015-02-09 15:12:09',
'visit' => 1,
'bounce' => 0
),
);
The result I expect must be when I do foreach:
date visit bounce
2015-02-06 2 1
2015-02-07 1 1
2015-02-08 2 2
2015-02-09 1 0
Here is the code what I've tried. But it just return the date count only.
$items = array_column($array, 'date');
$preg = preg_quote('2015-02-06', '~');
$result = preg_grep('~' . $preg . '~', $items);
echo 'Date <br/>' . count($result);
Please help, thank you in advanced.
You can create a new array:
$newArr = [];
foreach($array as $arr){
$d = (new DateTime($arr['date']))->format('Y-m-d');
$newArr[$d] = [
"visit" => isset($newArr[$d]['visit']) ? $newArr[$d]['visit'] += $arr['visit'] : $arr['visit'],
"bounce" => isset($newArr[$d]['bounce']) ? $newArr[$d]['bounce'] += $arr['bounce'] : $arr['bounce']
];
}
echo "<pre>";
var_dump($newArr);
echo "</pre>";
Above returns a nice formatted array which you can easily read out in the example you posted:
array(4) {
["2015-02-06"]=>
array(2) {
["visit"]=>
int(2)
["bounce"]=>
int(1)
}
["2015-02-07"]=>
array(2) {
["visit"]=>
int(1)
["bounce"]=>
int(1)
}
["2015-02-08"]=>
array(2) {
["visit"]=>
int(2)
["bounce"]=>
int(2)
}
["2015-02-09"]=>
array(2) {
["visit"]=>
int(1)
["bounce"]=>
int(0)
}
}
You could merge your entries like this:
$items = [];
foreach ($array as $value) {
$date = substr($value['date'], 0, 10);
if (!isset($items[$date]) {
$items[$date] = [
'date' => $date,
'visit' => 0,
'bounce' => 0
];
}
$items[$date]['visit'] += $value['visit'];
$items[$date]['bounce'] += $value['bounce'];
}
By using date
as a key in the $items
array, we make sure we sum up the visit
and bounce
for each date instead of appending them.
If you'd ever want to get rid of the keys, you can simply use array_values
.
If your array is called $myArray, and the new array your creating is $myDateArray:
$myDateArray = array();
foreach ($myArray as $value)
{
list($date_string, $other) = explode(" ", $value['date']);
if (array_key_exists($date_string, $myDateArray))
{
//adding the visits and bounces
$myDateArray[$date_string]['visit'] = $myDateArray[$date_string]['visit'] + $value['visit'];
$myDateArray[$date_string]['bounce'] = $myDateArray[$date_string]['bounce'] + $value['bounce'];
}
else
{
//putting the first values (of the key $date_string) into $myDateArray
array_push($myDateArray, array($date_string, $value['visit], $value['bounce']));
}
}
Let me know if that worked for you!
The code creates a new array which contains the summed result of your array.
$resultArray = [];
foreach($array as $row){
$dateObj = DateTime::createFromFormat('Y-m-d H:i:s', $row['date']);
$key = $dateObj->format('Y-m-d');
if(!array_key_exists($key, $resultArray)){
$resultArray[$key] = ['visit' => $row['visit'], 'bounce' => $row['bounce']];
}
else {
$resultArray[$key]['visit'] += $row['visit'];
$resultArray[$key]['bounce'] += $row['bounce'];
}
}
Result:
array (size=4)
'2015-02-06' =>
array (size=2)
'visit' => int 2
'bounce' => int 1
'2015-02-07' =>
array (size=2)
'visit' => int 1
'bounce' => int 1
'2015-02-08' =>
array (size=2)
'visit' => int 2
'bounce' => int 2
'2015-02-09' =>
array (size=2)
'visit' => int 1
'bounce' => int 0
Does this do the trick ?
<?php
$array = array (
0 => array(
'date' => '2015-02-06 10:42:39',
'visit' => 1,
'bounce' => 0
),
1 => array(
'date' => '2015-02-06 13:23:21',
'visit' => 1,
'bounce' => 1
),
2 => array(
'date' => '2015-02-07 04:11:42',
'visit' => 1,
'bounce' => 1
),
3 => array(
'date' => '2015-02-08 11:35:28',
'visit' => 1,
'bounce' => 1
),
4 => array(
'date' => '2015-02-08 15:12:09',
'visit' => 1,
'bounce' => 1
),
5 => array(
'date' => '2015-02-09 15:12:09',
'visit' => 1,
'bounce' => 0
),
);
$results = [];
foreach($array as $e)
{
$date = explode(' ',$e['date'])[0] ;
if(!isset($results[$date]))
$results[$date] = ['visit'=>0 , 'bounce'=>0] ;
$results[$date]['visit'] += $e['visit'];
$results[$date]['bounce'] += $e['bounce'];
}
print_r($results);
Solution using array_walk
, substr
and array_values
functions:
$date_keys = [];
array_walk($array, function($v) use(&$date_keys){
$datePart = $v['date'] = substr($v["date"], 0, 10);
if (isset($date_keys[$datePart])) {
$date_keys[$datePart]['visit'] += $v['visit'];
$date_keys[$datePart]['bounce'] += $v['bounce'];
} else {
$date_keys[$datePart] = $v;
}
});
print_r(array_values($date_keys));
The output:
Array
(
[0] => Array
(
[date] => 2015-02-06
[visit] => 2
[bounce] => 1
)
[1] => Array
(
[date] => 2015-02-07
[visit] => 1
[bounce] => 1
)
[2] => Array
(
[date] => 2015-02-08
[visit] => 2
[bounce] => 2
)
[3] => Array
(
[date] => 2015-02-09
[visit] => 1
[bounce] => 0
)
)
Technique/ Mechnism
$result = array();
$items = array_column($array, 'date');
for($i=0; $i < count($items); $i++){
$date = date("Y-m-d", strtotime($items[$i]));
$result[$date][0] += $array[$i]['visit'];
$result[$date][1] += $array[$i]['bounce'];
}
Result
Please design your output as required.
echo "date visit bounce<br/>";
foreach($result as $key => $value){
echo $key." ".$value[0]." ".$value[1]."<br/>";
}
Output
date visit bounce
2015-02-06 2 1
2015-02-07 1 1
2015-02-08 2 2
2015-02-09 1 0
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.