【发布时间】:2016-10-07 05:22:25
【问题描述】:
在这个函数的输出(最后)中,您可以看到产品被放置在每个子数组中。我想通过需要添加产品的参数来传递函数。参数将是不同页面上的变量:例如,如果变量的值为“meal2” => product 只需要添加到数组“meal2”下。
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
)
)
【问题讨论】: