嗨,我需要一些关于使用爆炸和array_component形成数组的帮助。
我正在尝试组合下面的两个数组(数量和食物描述),这是使用爆炸的结果。我试图使用数组组合使qty数组成为键,使fooddesc数组成为值,但无法正确语法,所以我放弃了。
我似乎不知道该怎么做。可能我只需要改变查询数据的方式,这样我就根本不需要使用爆炸??它把我逼疯了。
我形成了一个数组,包含一个包含n顿饭的用餐计划,其中包含食物的数量及其描述,这样我就可以很容易地将数组中的数据预先写入我的html。
这是我的代码和数组输出:
// Begin Meal Plan
$sql = "SELECT notes, mealdesc, optdesc, group_concat(qty) as qty, group_concat(fooddesc SEPARATOR ',') as fooddesc
FROM plan p
INNER JOIN mealplan mp
ON mp.planid = p.id
INNER JOIN meal m
ON m.id = mp.mealid
INNER JOIN foodmeal fm
ON fm.mealid = m.id
INNER JOIN food f
ON f.id = fm.foodid
where userid = 2
GROUP by mealdesc
order by mealdesc";
$result = mysqli_query($link, $sql);
if (!$result)
{
echo 'Error';
exit();
}
// Fetch meal data
while($row = mysqli_fetch_array($result)){
$meals[$row['mealdesc']] = array('optdesc' => $row['optdesc'], 'qty' => explode(",", $row['qty']), 'fooddesc' => explode(",", $row['fooddesc']));
$notes = $row['notes'];
}
// End Meal Plan
这是它产生的数组:(我把它剪掉,只显示第一顿饭,以保持简短)
Array
(
[Meal 1] => Array
(
[optdesc] => upon awakening
[qty] => Array
(
[0] => 1
[1] => 6
[2] => 1/2 cup
[3] => 2 slices
[4] => 1 piece
[5] => 1 cup
)
[fooddesc] => Array
(
[0] => egg
[1] => egg whites
[2] => oatmeal
[3] => wheat toast
[4] => fruit
[5] => berries
)
)
编辑再次尝试array_comport,并使用count和for循环来组合数量和食物描述,然后使用unset删除旧的数量/fooddesc数组。
我不知道这是否是最好的解决方案,但它有效。:)我想我只是回答了我自己的问题。
// Fetch meal data
while($row = mysqli_fetch_array($result)){
$meals[$row['mealdesc']] = array('optdesc' => $row['optdesc'], 'qty' => explode(",", $row['qty']), 'fooddesc' => explode(",", $row['fooddesc']));
$notes = $row['notes'];
}
$j = count($meals);
for($i = 1; $i <= $j ; $i++)
{
$meals['Meal '.$i][food] = array_combine($meals['Meal '.$i]['qty'], $meals['Meal '.$i]['fooddesc']);
unset($meals['Meal '.$i]['qty']);
unset($meals['Meal '.$i]['fooddesc']);
}
// End Meal Plan
这给了我:
Array
(
[Meal 1] => Array
(
[optdesc] => upon awakening
[food] => Array
(
[1] => egg
[6] => egg whites
[1/2 cup] => oatmeal
[2 slices] => wheat toast
[1 piece] => fruit
[1 cup] => berries
)
)
[Meal 2] => Array
(
[optdesc] => 3 hours later
[food] =>
)
array_combine(
array_values($meals['Meal 1']['qty']),
array_values($meals['Meal 1']['fooddesc'])
)
aray_combine()使用第一个参数作为键,第二个参数作为值,这里我们使用第一个数组的值作为组合数组的键。
再次尝试array_component,并使用count和for循环来组合数量和食物描述,然后使用unset删除旧的数量/fooddesc数组。
我不知道这是否是最好的解决方案,但它有效。:)我想我只是回答了我自己的问题。
// Fetch meal data
while($row = mysqli_fetch_array($result)){
$meals[$row['mealdesc']] = array('optdesc' => $row['optdesc'], 'qty' => explode(",", $row['qty']), 'fooddesc' => explode(",", $row['fooddesc']));
$notes = $row['notes'];
}
$j = count($meals);
for($i = 1; $i <= $j ; $i++)
{
$meals['Meal '.$i][food] = array_combine($meals['Meal '.$i]['qty'], $meals['Meal '.$i]['fooddesc']);
unset($meals['Meal '.$i]['qty']);
unset($meals['Meal '.$i]['fooddesc']);
}
// End Meal Plan
这给了我:
Array
(
[Meal 1] => Array
(
[optdesc] => upon awakening
[food] => Array
(
[1] => egg
[6] => egg whites
[1/2 cup] => oatmeal
[2 slices] => wheat toast
[1 piece] => fruit
[1 cup] => berries
)
)
[Meal 2] => Array
(
[optdesc] => 3 hours later
[food] =>
)