1
在這個函數的輸出中(最後)你可以看到產品被放置在每個子數組中。我想通過一個參數在需要添加產品的地方傳遞函數。該參數將是不同頁面上的變量:如果變量的值爲「meal2」=>產品只需要添加到數組「meal2」下。PHP - 數組錯誤輸出(需要函數幫助)
function addToMeal() {
$xmaaltijden = $_SESSION['xmaaltijden'];
$i=1;
while ($i <= $xmaaltijden) {
$schemas = array('maaltijd' . $i);
$i++;
$producten = $_SESSION['products'];
foreach ($schemas as $key => $schema){
foreach ($producten as $k => $product){
$variables[$schema][] = $product;
};
};
};
echo '<pre>' . print_r($variables,1) . '</pre>';
}
輸出:
Array
(
[meal1] => Array
(
[0] => 1
[1] => 3
[2] => 2
[3] => 9
)
[meal2] => Array
(
[0] => 1
[1] => 3
[2] => 2
[3] => 9
)
[meal3] => Array
(
[0] => 1
[1] => 3
[2] => 2
[3] => 9
)
)
我怎麼可以打印一切? – Mette
print_r($ variables); – nospor
不,我只看到陣列,我添加了產品.... – Mette