0

嗨,我需要一些帮助来使用explode 和array_combine 形成数组。

我正在尝试组合下面的两个数组(数量和 fooddesc),它们是使用爆炸的结果。我试图使用数组组合将 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_combine 并使用 count 和 for 循环为餐点组合 qty 和 food 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] => 
        )
4

2 回答 2

0

再次尝试 array_combine 并使用 count 和 for 循环来合并 qty 和 food 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] => 
        )
于 2013-02-13T22:52:43.023 回答
0
array_combine(
array_values($meals['Meal 1']['qty']), 
array_values($meals['Meal 1']['fooddesc'])
)

aray_combine()使用第一个参数作为键,第二个参数作为值,这里我们使用第一个数组的值作为组合数组的键。

于 2013-02-13T14:29:14.160 回答