通过多维阵列输出组PHP foreach循环由一个键 - >值

问题描述 投票:0回答:3

我有一天totalprice以下数组:

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

如何添加时totalprice正在发生变化,所以我可以有一个日期:

01/08/2019
31/08/2019
585.00
01/09/2019
30/09/2019
410.00
01/10/2019
31/10/2019
235.00
php arrays multidimensional-array foreach
3个回答
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


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

0
投票

试试这个,我改名为你的数组$率和切换到一个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;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.