У меня возникла проблема с циклом foreach внутри цикла for, который выполняется только при первом запуске цикла for. Мне нужно, чтобы он запускался каждый раз.PHP foreach внутри for loop работает только один раз
У меня есть следующий код:
for ($numberId = 0; $numberId <= count($items[0])-1; $numberId++) {
echo 'numberId is '.$numberId;
var_dump($items[0][$numberId]);
foreach ($records as $bottle) {
echo 'bottle array is:';
var_dump($bottle);
}
}
Выход для этого:
numberId is 0
string(19) "some bottle"
bottle array is:array(6) {
["market_name"]=>
string(47) "another bottle"
[0]=>
string(47) "another bottle"
["avg_price_7_days"]=>
string(6) "137.99"
[1]=>
string(6) "137.99"
["current_price"]=>
string(6) "134.06"
[2]=>
string(6) "134.06"
}
bottle array is:array(6) {
["market_name"]=>
string(50) "some other bottle"
[0]=>
string(50) "some other bottle"
["avg_price_7_days"]=>
string(4) "2.95"
[1]=>
string(4) "2.95"
["current_price"]=>
string(4) "2.92"
[2]=>
string(4) "2.92"
}
bottle array is:array(6) {
["market_name"]=>
string(33) "a third bottle"
[0]=>
string(33) "a third bottle"
["avg_price_7_days"]=>
string(4) "1.25"
[1]=>
string(4) "1.25"
["current_price"]=>
string(4) "1.22"
[2]=>
string(4) "1.22"
}
numberId is 1string(27) "a new bottle"
numberId is 2string(32) "some really old bottle"
numberId is 3string(47) "another bottle"
numberId is 4string(28) "some other bottle"
numberId is 5string(29) "a third bottle"
Как вы можете видеть, после первого запуска через цикл, только выше Еогеасп кода выполняется для остальных прогонов. Почему это?
Вы Вытряхивали переменные, связанные? Результат хорош, но не имеет смысла для решения проблемы. Как насчет сброса '$ items' и' $ records' вместо – Machavity