Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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" } } 
share|improve this question
    
So where is is it wrong? I see three foreach loops here –  mituw16 Jan 13 at 20:10
    
in the third loop $amt is always = 0. Say for example on the 1st foreach instance $amt=24 the output in the third loop is 0 –  David Jan 13 at 20:16
    
The code just doesn't make any sense. Why are you adding a new entry to the $listdata array in every top loop and then looping through ALL its entries in the second foreach? Regarding your question, surely the data at some point gets mangled by another loop. E.g. $price or $disc get set to 0 affecting all the subsequent usages. –  Andris Jan 13 at 20:16
    
We don't know what's in your variables. Start with var_dump($query); and var_dump($listdata); before your return statement. –  sjagr Jan 13 at 20:22
    
I have database rows with start date, finish date for a special offer and a discount to be applied. For each row i want a list of dates with the discounted amount for a javascript function later. the 1st loop takes the row and the discount, the 2nd enters the array of results and the 3rd actually gets to the data i need (the dates in this case) and should assign amt to each date. –  David Jan 13 at 20:23

1 Answer 1

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.