简体   繁体   中英

PHP nested foreach loop on an array

I have nested foreach loops. In the top loop the variable $amt is correctly output. When it goes into the second level it is still correct, however in the third level it is suddenly 0.

What am I doing wrong?

$listdata = array();

foreach ($query as $item) {
    unset($listdata); //----------it was adding to itself each loop replacing orig vals
    $listdata[] = getDatesinbetween($item['start_date'], $item['finish_date']);
    $disc = 100 - (int) $item['discount'];

    $disc = $disc / 100;
    $amt  = $price * $disc;

    //amount set here
    $amt  = number_format($amt, 2);

    echo $amt; //correct

    foreach ($listdata as $data => $key) {
        echo $amt; // correct

        foreach ($key as $ky) {
            echo $amt; // 0!
            $lists[$ky] = "Only €" . $amt;
        }
    }
}

return $lists;

Everything else is correct just the $amt variable is wrong!

vardump of $query

array(3) { [0]=> array(9) { 
    ["id"]=> string(1) "6" ["postdate"]=> string(19) "2014-01- 13 14:23:34" ["name"]=> string(0) "" ["start_date"]=> string(19) "2014-04-01 00:00:00" ["finish_date"]=> string(19) "2014-04-10 00:00:00" ["location"]=> string(3) "All" ["type"]=> string(17) "Facebook Discount" ["discount"]=> string(2) "50" ["offerid"]=> string(1) "7" }
[1]=> array(9) { 
    ["id"]=> string(1) "3" ["postdate"]=> string(19) "2014-01-05 17:53:22" ["name"]=> string(0) "" ["start_date"]=> string(19) "2014-01-06 00:00:00" ["finish_date"]=> string(19) "2014-01-31 00:00:00" ["location"]=> string(3) "All" ["type"]=> string(17) "Facebook Discount" ["discount"]=> string(3) "100" ["offerid"]=> string(1) "4" } 
[2]=> array(9) { 
    ["id"]=> string(1) "4" ["postdate"]=> string(19) "2014-01-05 18:51:27" ["name"]=> string(0) "" ["start_date"]=> string(19) "2014-02-01 00:00:00" ["finish_date"]=> string(19) "2014-02-28 00:00:00" ["location"]=> string(3) "All" ["type"]=> string(17) "Facebook Discount" ["discount"]=> string(3) "100" ["offerid"]=> string(1) "5" } }

var_dump of listdata

array(1) { [0]=> array(10) { 
    [0]=> string(10) "2014/04/01" [1]=> string(10) "2014/04/02" [2]=> string(10) "2014/04/03" [3]=> string(10) "2014/04/04" [4]=> string(10) "2014/04/05" [5]=> string(10) "2014/04/06" [6]=> string(10) "2014/04/07" [7]=> string(10) "2014/04/08" [8]=> string(10) "2014/04/09" [9]=> string(10) "2014/04/10" } }
24.5024.5024.5024.5024.5024.5024.5024.5024.5024.5024.5024.50
array(2) { 
    [0]=> array(10) { 
        [0]=> string(10) "2014/04/01" [1]=> string(10) "2014/04/02" [2]=> string(10) "2014/04/03" [3]=> string(10) "2014/04/04" [4]=> string(10) "2014/04/05" [5]=> string(10) "2014/04/06" [6]=> string(10) "2014/04/07" [7]=> string(10) "2014/04/08" [8]=> string(10) "2014/04/09" [9]=> string(10) "2014/04/10" } 
    [1]=> array(26) { 
        [0]=> string(10) "2014/01/06" [1]=> string(10) "2014/01/07" [2]=> string(10) "2014/01/08" [3]=> string(10) "2014/01/09" [4]=> string(10) "2014/01/10" [5]=> string(10) "2014/01/11" [6]=> string(10) "2014/01/12" [7]=> string(10) "2014/01/13" [8]=> string(10) "2014/01/14" [9]=> string(10) "2014/01/15" [10]=> string(10) "2014/01/16" [11]=> string(10) "2014/01/17" [12]=> string(10) "2014/01/18" [13]=> string(10) "2014/01/19" [14]=> string(10) "2014/01/20" [15]=> string(10) "2014/01/21" [16]=> string(10) "2014/01/22" [17]=> string(10) "2014/01/23" [18]=> string(10) "2014/01/24" [19]=> string(10) "2014/01/25" [20]=> string(10) "2014/01/26" [21]=> string(10) "2014/01/27" [22]=> string(10) "2014/01/28" [23]=> string(10) "2014/01/29" [24]=> string(10) "2014/01/30" [25]=> string(10) "2014/01/31" } } 

As updated above, each time the loop ran it simply added to the array $listdata meaning the previous values of dates where being overwritten by the next. Added unset() in the 1st loop.

    $listdata= array();

    foreach ($query as $item) {// looping through 3 rows
        unset($listdata);
        $listdata[] =getDatesFromRange($item['start_date'],$item['finish_date']);
        $amt=0;
        $disc= 100 - (int)$item['discount'];

        $disc= $disc / 100;
        $amt= $price * $disc;
//amount set here
        $amt= number_format($amt,2);

     foreach ($listdata as $data => $key) {

         foreach ($key as $ky) {
            $lists[$ky]= $amt; 
         }
       }
     }
     var_dump($lists); //final output need i.e. assoc array of dates/discounts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM