显示每月分组的销售总数

时间:2017-12-03 12:23:06

标签: php arrays

我之前发过这个问题,有人在这里帮助我接近解决方案,但我似乎无法像我需要的那样显示数据。

我有这个数组$sales,其结构如下:

array(627) (

[0] => array(4) (
    [id]=> (int) 1
    [name] => (string) Tesla
    [total]=> (string) 24
    [month] => (string) 4
  )

[1] => array(4) (
    [id]=> (int) 1
    [name] => (string) Tesla
    [total]=> (string) 18
    [month] => (string) 5


[2] => array(4) (
    [id]=> (int) 1
    [name] => (string) Tesla
    [total]=> (string) 18
    [month] => (string) 6

[3] => array(4) (
    [id]=> (int) 2
    [name] => (string) Audi
    [total]=> (string) 16
    [month] => (string) 4
  )

[4] => array(4) (
    [id]=> (int) 2
    [name] => (string) Audi
    [total]=> (string) 18
    [month] => (string) 5

它的作用是存储每种类型汽车和每个月的销售信息。有些月份没有销售,所以他们是空白。 我想要做的是循环遍历每种类型的汽车并在html表中显示如下所示的销售:

Car      Jan. Feb. Mar. Apr. May. Jun. Jul, Aug. Sept. Oct. Nov. Dec
Tesla    0    0    0    24   18   18   0    0     0    0    0     0
Audi     0    0    0    16   18    0   0    0     0    0    0     0

问题是,我只显示月数和月数,如果那个月有销售的话。例如在特斯拉的案例中,仅在第4,5,6个月有销售,而我又错过了9个月。

我得到的帮助是:

$result = [];
foreach($sales as $sale) {
    if(!isset($result[$sale['name']]))
       $result[$sale['name']] = [];

    if(!isset($result[$sale['name']][$sale['month']]))
       $result[$sale['name']][$sale['month']] = $sale['total'];
    else
       $result[$sale['name']][$sale['month']] += $sale['total'];
}

这很好,但是如何用这个数组映射一年中的每个月?如何在没有销售的情况下显示0个月?

我希望你能帮助我。对我来说有点复杂。

更新:

foreach($cars as $car){
        echo '<tr><td>'.$car .'</td>';
            for($i=1;$i<=12;$i++){
            if (isset($new[$i][$car])){
                echo '<td>'.$new[$i][$car] . '</td>';
            }else{
                echo '<td>'."0".'</td>';
            }   
        }      
       echo '</tr>';
}

3 个答案:

答案 0 :(得分:0)

尝试将长度为12的数组初始化为0.如果相应索引的月份有值,则更改它,否则显示初始值本身

答案 1 :(得分:0)

这是一种方法,首先构建一个以月为第一键的新数组,然后是一个新的子数组,其中car为key,值为sales。

然后我使用array_column抓取所有汽车,并使用array_unique对重复项进行排序。

然后我循环月份(1-> 12)并输出日期()月份 然后我循环汽车并输出值(销售额)。

foreach($sales as $sale){
    $new[$sale["month"]][$sale["name"]] = $sale["total"];
}

$cars = array_unique(array_column($sales, "name", "id"));
$cars[] = "Sum"; // add a car named "Sum" for later use of displaying the sum
//Var_dump($cars);
echo "<table>\n<th>Car</th>";

for($i=1;$i<=12;$i++){
    echo "<th>" . date("M", strtotime("2017-$i-01")) . "</th>"; // create header
    foreach($cars as $car){ // loop through the cars and add zero values to months if there is no sales on each car.
        if(!isset($new[$i][$car])){
            $new[$i][$car] = 0; // add months with 0 values
        }
    }
    $new[$i]["Sum"] = array_sum($new[$i]); // sum the month sales and add it to car "Sum"
}
ksort($new); // sort it on keys (months);

//output each car in a table (including "Sum") using array_column and implode
foreach($cars as $key => $car){
    If($car != "Sum"){
        echo "\n<tr><td><a href='$key'>$car</a></td><td>" . implode("</td><td>", array_column($new, $car)). "</td></tr>";
    }Else{
        echo "\n<tr><td>$car</td><td>" . implode("</td><td>", array_column($new, $car)). "</td></tr>"; 
    }
}

echo "</table>";

对于php测试:
https://3v4l.org/tZNOK
要查看实际输出是什么:
https://jsfiddle.net/gfjdcj4r/

编辑,添加了array_sum 编辑;添加了html表 编辑;使用array_column的第三个选项将id添加为锚 这使得$ cars的钥匙成为了id 然后将其作为锚点输出。

答案 2 :(得分:-1)

迭代您的$ result数组,并为每个&#34; car&#34;迭代1到12,并且每个月检查它是否存在于&#34; car&#34;结果内部数组

$result = [];
foreach($sales as $sale) {
    if(!isset($result[$sale['name']]))
        $result[$sale['name']] = [];

    if(!isset($result[$sale['name']][$sale['month']]))
        $result[$sale['name']][$sale['month']] = $sale['total'];
    else
        $result[$sale['name']][$sale['month']] += $sale['total'];
}

$totalSales = array();
foreach ($result AS $car=>$monthSales) {
    print $car;
    for($i=1;$i<=12;$i++) {
        if(array_key_exists($i, $monthSales)) {
            print $monthSales[$i]; // put here the names
        }
        else {
            print "0";
        }

        if(array_key_exists($i, $totalSales)) {
            $totalSales[$i] = 0;
        }
        $totalSales[$i] += (isset($monthSales[$i])) ? $monthSales[$i] : 0;
    }
}

for($i=1;$i<=12;$i++) {
   if(array_key_exists($i, $totalSales)) {
       print $totalSales[$i]; // put here the names
   }
   else {
       print "0";
   }
}
相关问题