我有嵌套foreach
循环。在顶部循环中,正确输出变量$amt
。当它进入第二级时它仍然是正确的,但是在第三级它突然变为0。
我做错了什么?
$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;
其他一切都是正确的$amt
变量是错误的!
$ query的vardump
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" } }
listdata的var_dump
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" } }
答案 0 :(得分:1)
如上所述,每次循环运行时,它只是添加到数组$ listdata中,这意味着之前被下一个覆盖的日期值。在第一个循环中添加了unset()。
$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