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