如何按日期对此PHP数组进行分组并对值求和

时间:2015-11-18 01:29:07

标签: php arrays

我有这个PHP数组 - 我需要按日期分组,我假设通过创建另一个数组,并将每个日期的成人数加起来

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
<div>
  <%= f.input :username, required: true, autofocus: true %>
    <%= f.input :email, required: true %>

<% if request.fullpath.include?('user') %>
       <%= f.input :teacher_id, required: false %>
<% end %>


    <%= f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length) %>
    <%= f.input :password_confirmation, required: false %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Sign up" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

所以期望的输出就像这样(也按日期排序):

Array
(
[0] => stdClass Object
    (
        [thedate] => 2016-04-09
        [theadults] => 5
    )

[1] => stdClass Object
    (
        [thedate] => 2016-04-09
        [theadults] => 8
    )

[2] => stdClass Object
    (
        [thedate] => 2016-04-09
        [theadults] => 12
    )

[3] => stdClass Object
    (
        [thedate] => 2016-03-05
        [theadults] => 1
    )

[4] => stdClass Object
    (
        [thedate] => 2016-02-27
        [theadults] => 1
    )

[5] => stdClass Object
    (
        [thedate] => 2016-03-05
        [theadults] => 1
    )

[6] => stdClass Object
    (
        [thedate] => 2016-03-05
        [theadults] => 1
    )

[7] => stdClass Object
    (
        [thedate] => 2016-04-09
        [theadults] => 3
    )

)

有人能告诉我最有效的方法吗? 提前感谢您感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

看起来最简单的方法是将计数聚合到一个以日期为关键字的数组中。然后,您可以对密钥进行排序,然后根据需要输出数据。

考虑一下:

$totalsByDate = [];
foreach ($array as $element) if (($date = strtotime($element->thedate)) and is_numeric($element->theadults)) {
    $totalsByDate[$date] += $element->theadults;
}
ksort($totalsByDate);
foreach ($totalsByDate as $date => $total) {
    echo "On " . date("Y-m-d", $date) . " there were " . $total " adults.\n";
}

如果您希望日期按降序排序,也可以将ksort()替换为krsort()

答案 1 :(得分:0)

你有:
  • 包含$adults的数组,其中每个$adult都是具有thedatetheadults属性的对象。

现在您要创建$groupedAdults,这将是使用thedate属性作为键的数组。因此逻辑可能如下所示:

  1. foreach($adults$adult):
  2. $date = $adult->thedate
  3. 检查是否array_key_exists($date, $groupedAdults)
  4. 如果不是,请将当前$adult复制到$groupedAdults[$date]
  5. 如果是,请将$adult->theadults添加到$groupedAdults[$date]->adults
  6. 尝试一下,希望你现在可以自己编写它。

答案 2 :(得分:0)

尝试以下内容

<?php

$array= array(
    array("thedate"=>"2016-03-05","theadults"=>10),
    array("thedate"=>"2016-03-05","theadults"=>1),
    array("thedate"=>"2016-03-06","theadults"=>1),
    array("thedate"=>"2016-03-07","theadults"=>1),
    array("thedate"=>"2016-03-05","theadults"=>1),
    array("thedate"=>"2016-03-07","theadults"=>1),
    array("thedate"=>"2016-03-06","theadults"=>1)
    );

print_r($array);
$new = array();
for($i=0;$i < count($array);$i++)
{
    $index = -1;
    for($j=0;$j<count($new);$j++)
    {
        if($array[$i]['thedate'] == $new[$j]['thedate'])
        {
            $index = $j;
            breck;
        }
    }
    if($index == -1)
    {
        array_push($new, $array[$i]);
    }
    else
    {
        $new[$index]['theadults'] += $array[$i]['theadults'];
    }
}
echo "<hr>";
print_r($new);