PHP foreach循环通过多维数组输出按一个键->值分组

时间:2019-02-04 21:40:27

标签: php arrays multidimensional-array foreach

我有以下带有天数和总价的数组:

const input = [4,5,5,4,5,3,3,5,4,3];

const output = input.reduce((accum, x) => {
  accum[x] = accum[x] ? accum[x] + 1 : 1;
  return accum;
}, {});

console.log(output);

使用以下代码:

Array ( 
[0] => Array ( [day] => 01/08/2019 [totalprice] => 585.00 ) 
[1] => Array ( [day] => 02/08/2019 [totalprice] => 585.00 ) 
[2] => Array ( [day] => 03/08/2019 [totalprice] => 585.00 ) 
[3] => Array ( [day] => 04/08/2019 [totalprice] => 585.00 ) 
[4] => Array ( [day] => 05/08/2019 [totalprice] => 585.00 ) 
[5] => Array ( [day] => 06/08/2019 [totalprice] => 585.00 ) 
[6] => Array ( [day] => 07/08/2019 [totalprice] => 585.00 ) 
[7] => Array ( [day] => 08/08/2019 [totalprice] => 585.00 ) 
[8] => Array ( [day] => 09/08/2019 [totalprice] => 585.00 ) 
[9] => Array ( [day] => 10/08/2019 [totalprice] => 585.00 ) 
[10] => Array ( [day] => 11/08/2019 [totalprice] => 585.00 ) 
[11] => Array ( [day] => 12/08/2019 [totalprice] => 585.00 ) 
[12] => Array ( [day] => 13/08/2019 [totalprice] => 585.00 ) 
[13] => Array ( [day] => 14/08/2019 [totalprice] => 585.00 ) 
[14] => Array ( [day] => 15/08/2019 [totalprice] => 585.00 ) 
[15] => Array ( [day] => 16/08/2019 [totalprice] => 585.00 ) 
[16] => Array ( [day] => 17/08/2019 [totalprice] => 585.00 ) 
[17] => Array ( [day] => 18/08/2019 [totalprice] => 585.00 ) 
[18] => Array ( [day] => 19/08/2019 [totalprice] => 585.00 ) 
[19] => Array ( [day] => 20/08/2019 [totalprice] => 585.00 ) 
[20] => Array ( [day] => 21/08/2019 [totalprice] => 585.00 ) 
[21] => Array ( [day] => 22/08/2019 [totalprice] => 585.00 ) 
[22] => Array ( [day] => 23/08/2019 [totalprice] => 585.00 ) 
[23] => Array ( [day] => 24/08/2019 [totalprice] => 585.00 ) 
[24] => Array ( [day] => 25/08/2019 [totalprice] => 585.00 ) 
[25] => Array ( [day] => 26/08/2019 [totalprice] => 585.00 ) 
[26] => Array ( [day] => 27/08/2019 [totalprice] => 585.00 ) 
[27] => Array ( [day] => 28/08/2019 [totalprice] => 585.00 ) 
[28] => Array ( [day] => 29/08/2019 [totalprice] => 585.00 ) 
[29] => Array ( [day] => 30/08/2019 [totalprice] => 585.00 ) 
[30] => Array ( [day] => 31/08/2019 [totalprice] => 585.00 ) 
[31] => Array ( [day] => 01/09/2019 [totalprice] => 410.00 ) 
[32] => Array ( [day] => 02/09/2019 [totalprice] => 410.00 ) 
[33] => Array ( [day] => 03/09/2019 [totalprice] => 0.00 ) 
[34] => Array ( [day] => 04/09/2019 [totalprice] => 0.00 ) 
[35] => Array ( [day] => 05/09/2019 [totalprice] => 0.00 ) 
[36] => Array ( [day] => 06/09/2019 [totalprice] => 0.00 ) 
[37] => Array ( [day] => 07/09/2019 [totalprice] => 0.00 ) 
[38] => Array ( [day] => 08/09/2019 [totalprice] => 0.00 ) 
[39] => Array ( [day] => 09/09/2019 [totalprice] => 0.00 ) 
[40] => Array ( [day] => 10/09/2019 [totalprice] => 0.00 ) 
[41] => Array ( [day] => 11/09/2019 [totalprice] => 0.00 ) 
[42] => Array ( [day] => 12/09/2019 [totalprice] => 0.00 ) 
[43] => Array ( [day] => 13/09/2019 [totalprice] => 0.00 ) 
[44] => Array ( [day] => 14/09/2019 [totalprice] => 0.00 ) 
[45] => Array ( [day] => 15/09/2019 [totalprice] => 0.00 ) 
[46] => Array ( [day] => 16/09/2019 [totalprice] => 0.00 ) 
[47] => Array ( [day] => 17/09/2019 [totalprice] => 410.00 ) 
[48] => Array ( [day] => 18/09/2019 [totalprice] => 410.00 ) 
[49] => Array ( [day] => 19/09/2019 [totalprice] => 410.00 ) 
[50] => Array ( [day] => 20/09/2019 [totalprice] => 410.00 ) 
[51] => Array ( [day] => 21/09/2019 [totalprice] => 410.00 ) 
[52] => Array ( [day] => 22/09/2019 [totalprice] => 410.00 ) 
[53] => Array ( [day] => 23/09/2019 [totalprice] => 410.00 ) 
[54] => Array ( [day] => 24/09/2019 [totalprice] => 410.00 ) 
[55] => Array ( [day] => 25/09/2019 [totalprice] => 410.00 ) 
[56] => Array ( [day] => 26/09/2019 [totalprice] => 410.00 ) 
[57] => Array ( [day] => 27/09/2019 [totalprice] => 410.00 ) 
[58] => Array ( [day] => 28/09/2019 [totalprice] => 410.00 ) 
[59] => Array ( [day] => 29/09/2019 [totalprice] => 410.00 ) 
[60] => Array ( [day] => 30/09/2019 [totalprice] => 410.00 ) 
[61] => Array ( [day] => 01/10/2019 [totalprice] => 235.00 ) 
[62] => Array ( [day] => 02/10/2019 [totalprice] => 235.00 ) 
[63] => Array ( [day] => 03/10/2019 [totalprice] => 235.00 ) 
[64] => Array ( [day] => 04/10/2019 [totalprice] => 235.00 ) 
[65] => Array ( [day] => 05/10/2019 [totalprice] => 235.00 ) 
[66] => Array ( [day] => 06/10/2019 [totalprice] => 235.00 ) 
[67] => Array ( [day] => 07/10/2019 [totalprice] => 235.00 ) 
[68] => Array ( [day] => 08/10/2019 [totalprice] => 235.00 ) 
[69] => Array ( [day] => 09/10/2019 [totalprice] => 235.00 ) 
[70] => Array ( [day] => 10/10/2019 [totalprice] => 235.00 ) 
[71] => Array ( [day] => 11/10/2019 [totalprice] => 235.00 ) 
[72] => Array ( [day] => 12/10/2019 [totalprice] => 235.00 ) 
[73] => Array ( [day] => 13/10/2019 [totalprice] => 235.00 ) 
[74] => Array ( [day] => 14/10/2019 [totalprice] => 235.00 ) 
[75] => Array ( [day] => 15/10/2019 [totalprice] => 235.00 ) 
[76] => Array ( [day] => 16/10/2019 [totalprice] => 235.00 ) 
[77] => Array ( [day] => 17/10/2019 [totalprice] => 235.00 ) 
[78] => Array ( [day] => 18/10/2019 [totalprice] => 235.00 ) 
[79] => Array ( [day] => 19/10/2019 [totalprice] => 235.00 ) 
[80] => Array ( [day] => 20/10/2019 [totalprice] => 235.00 ) 
[81] => Array ( [day] => 21/10/2019 [totalprice] => 235.00 ) 
[82] => Array ( [day] => 22/10/2019 [totalprice] => 235.00 ) 
[83] => Array ( [day] => 23/10/2019 [totalprice] => 235.00 ) 
[84] => Array ( [day] => 24/10/2019 [totalprice] => 235.00 ) 
[85] => Array ( [day] => 25/10/2019 [totalprice] => 235.00 ) 
[86] => Array ( [day] => 26/10/2019 [totalprice] => 235.00 ) 
[87] => Array ( [day] => 27/10/2019 [totalprice] => 235.00 ) 
[88] => Array ( [day] => 28/10/2019 [totalprice] => 235.00 ) 
[89] => Array ( [day] => 29/10/2019 [totalprice] => 235.00 ) 
[90] => Array ( [day] => 30/10/2019 [totalprice] => 235.00 ) 
[91] => Array ( [day] => 31/10/2019 [totalprice] => 235.00 )
) 

我得到:

foreach($blankarray as $rate){

    if ($rate['totalprice'] == '0'){
    } else {
        $firstrate = $rate['totalprice'];
        if ($firstrate != $previousrate) {
            $firstdate = $rate['day'];
            echo '<br>'.$firstdate; 
            echo '</br><font color="green"> <b>'.$firstrate .'</b></font>';
            $previousrate = $firstrate;
        }

总价变化时如何添加上一个日期,这样我就可以了:

01/08/2019
585.00
01/09/2019
410.00
01/10/2019
235.00

3 个答案:

答案 0 :(得分:0)

尝试一下,我将您的数组重命名为$ rates并切换到for循环。

$rates_count = sizeof($rates);
$previous = 1;
for($i = 1; $i < $rates_count; ++$i) {
    if($rates[$i]['totalprice'] !== 0) {
        if($rates[$i]['totalprice'] > $rates[$previous]['totalprice']) {
            echo
                '<br />', "\n",
                $rates[$i]['day'], "\n",
                '<br />', "\n",
                $rates[$previous]['day'], "\n",
                '<br />', "\n",
                '<span style="color: green; font-weight: bold;">', $rates[$i]['totalprice'], '</span>', "\n";
            $previous = $i;
        }
    }
}

答案 1 :(得分:0)

将上一个日期和价格保存在变量中。价格每变化一次,就打印前一个日期,上一个价格和当前日期。

$prev_date = null
foreach ($blankarray as $rate) {
    if ($rate['totalprice'] == 0) {
        // ignore zero rows
        continue;
    }
    if ($rate['day'] != $prev_date) {
        if ($prev_date) {
            echo "$prev_date<br><span style='color: green;'>$prev_price</span><br>";
        }
    }
    $prev_date = $rate['date'];
    $prev_price = $rate['totalprice'];
    echo $rate['date'] . "<br>";
}
// End the last group
echo "$prev_date<br><span style='color: green;'>$prev_price</span><br>";

答案 2 :(得分:0)

此代码将为您提供所需的结果。它检查数组中的当前价格是否等于前一个价格或下一个价格(忽略所有0值),如果是,则输出价格:

$previousprice = -1;
$thisprice = -1;
$nextprice = $blankarray[0]['totalprice'];
$c = count($blankarray);
for ($i = 0; $i < $c; $i++) {
    $previousprice = $thisprice;
    $thisprice = $nextprice;
    $nextprice = ($i == $c - 1) ? -1 : $blankarray[$i+1]['totalprice'];
    if ($previousprice == 0 || $thisprice == 0 || $nextprice == 0) continue;
    if ($thisprice != $previousprice) {
        echo '<br>'.$blankarray[$i]['day']; 
    }
    if ($thisprice != $nextprice) {
        echo '<br>'.$blankarray[$i]['day']; 
        echo '<br><font color="green"> <b>'.$blankarray[$i]['totalprice'].'</b></font>' . "\n";        
    }
}

输出:

<br>01/08/2019<br>31/08/2019<br><font color="green"> <b>585</b></font> 
<br>01/09/2019<br>30/09/2019<br><font color="green"> <b>410</b></font> 
<br>01/10/2019<br>31/10/2019<br><font color="green"> <b>235</b></font>

Demo on 3v4l.org