您好我需要帮助使用explode和array_combine形成数组。
我正在尝试将下面的两个数组(qty和fooddesc)组合使用,这是使用爆炸的结果。我试图使qty数组成为键,而fooddesc数组使用数组组合的值,但无法正确获取语法,所以我中止了。
我似乎无法弄清楚如何做到这一点。可能我只需要改变我查询数据的方式,这样我根本不需要使用爆炸?这让我疯了。
我正在组建一个包含膳食计划的阵列,其中包含大量食物及其描述,以便我可以轻松地将数据中的数据传送到我的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_combine并使用count和for循环来结合数量和食物desc,然后使用unset来删除旧的qty / 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] =>
)
答案 0 :(得分:0)
array_combine(
array_values($meals['Meal 1']['qty']),
array_values($meals['Meal 1']['fooddesc'])
)
aray_combine()使用第一个参数作为键,第二个参数作为值,这里我们使用第一个数组的值作为组合数组的键。
答案 1 :(得分:0)
再次尝试使用array_combine并使用count和for循环来合并数量和食物desc,然后使用unset来移除旧的qty / 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] =>
)