0

Hi I need some help with forming arrays using explode and array_combine.

I am trying to combine two arrays below (qty and fooddesc) that were a result of using explode. I was trying to make the qty array the key and the fooddesc array the value using array combine but can't get the syntax right so I aborted.

I can't seem to figure out how to do this. Possibly I just need to change the way I am querying the data so I don't need to use explode at all?? It's driving me nuts.

I was forming the array to contain a meal plan with n number of meals that contain quantities of food and their descriptions so that I can then easily foreach the data out of the array into my html.

Here is my code and the array output:

// 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

Here is the array that it produces: (I clipped this to show just the first meal to keep it short)

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
                )

        )

EDIT Tried array_combine again and used count and a for loop for the meals to combine the qty and food desc and then used unset to remove the old qty/fooddesc arrays.

I don't know if it is the best solution but it works. :) Think I just answered my own question.

// 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

This gives me:

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] => 
        )

2 Answers 2

0
array_combine(
array_values($meals['Meal 1']['qty']), 
array_values($meals['Meal 1']['fooddesc'])
)

aray_combine() uses first parameter for keys, and second parameters for values, here we used values for first array as key for combined array.

1
  • Thanks Akam. How would I do that if I don't always know the exact number of meals? The meals can vary per plan. Some may have 4, some may have 8. Commented Feb 13, 2013 at 14:50
0

Tried array_combine again and used count and a for loop for the meals to combine the qty and food desc and then used unset to remove the old qty/fooddesc arrays.

I don't know if it is the best solution but it works. :) Think I just answered my own question.

// 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

This gives me:

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] => 
        )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.