PHP总和多个数组值

时间:2016-04-21 08:39:55

标签: php arrays multidimensional-array

如何对多维数组值求和,然后将日期分组为我的代码。 如果有任何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
        ),

);

我期望的结果必须是我做的时候:

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

请帮忙,先谢谢你。

7 个答案:

答案 0 :(得分: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)
  }
}

答案 1 :(得分: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作为$items数组中的键,我们确保为每个日期总结visitbounce,而不是附加它们。

如果您想要删除密钥,只需使用array_values

答案 2 :(得分: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']));
  }
}

请告诉我这是否适合您!

答案 3 :(得分: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

答案 4 :(得分: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);

答案 5 :(得分: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
        )
)

答案 6 :(得分:0)

技术/机制

$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