2016-04-21 45 views
0

如何求和多維數組值,然後將其與日期分組作爲我的代碼。 如果有任何PHP代碼我應該嘗試什麼,請告訴我。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 
     ), 

); 

我希望當我這樣做的foreach必須是結果:

date   visit  bounce 
2015-02-06  2   1 
2015-02-07  1   1 
2015-02-08  2   2 
2015-02-09  1   0 

這裏是我試過的代碼。但它只是返回日期計數。

$items = array_column($array, 'date'); 
$preg = preg_quote('2015-02-06', '~'); 
$result = preg_grep('~' . $preg . '~', $items); 
echo 'Date <br/>' . count($result); 

請幫助,謝謝你的進步。

+1

是否來自數據庫的數組中的信息? – RiggsFolly

+0

你是在告訴自己你的結果應該與foreach結合,但是你自己的代碼不是foreach?爲什麼不? – izk

回答

0

使用溶液array_walksubstrarray_values功能:

$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)); 

輸出:

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 
     ) 
) 
+0

謝謝!這是選擇的答案。 – cocksparrer

+0

@cocksparrer,不客氣 – RomanPerekhrest

0

您可以創建一個新的數組:

$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>"; 

上述方法返回一個很好的格式化的數組,你可以很容易地在您發佈的例子讀出:

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) 
    } 
} 
0

你可以合併這樣的條目:

$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']; 
} 

通過使用date作爲中的關鍵字數組,我們確保每個日期總結visitbounce而不是追加它們。

如果你想擺脫鑰匙,你可以簡單地使用array_values

0

如果你的數組稱爲$ myArray的,而新的數組的創造是$ 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'])); 
    } 
} 

讓我知道這是否爲你工作!

0

該代碼創建一個新數組,其中包含數組的求和結果。

$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']; 
    } 
} 

結果:

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 
0

這是否做的伎倆?

<?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); 
0

技術/ 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']; 
} 

結果

請根據需要設計您的輸出。

echo "date&nbsp;&nbsp;&nbsp;visit&nbsp;&nbsp;&nbsp;bounce<br/>"; 
foreach($result as $key => $value){ 
    echo $key."&nbsp;&nbsp;&nbsp;".$value[0]."&nbsp;&nbsp;&nbsp;".$value[1]."<br/>"; 
} 

輸出

date   visit bounce 
2015-02-06  2  1 
2015-02-07  1  1 
2015-02-08  2  2 
2015-02-09  1  0