0

How would you modify the following code using only a foreach loop by replacing the keys as the values? For examples the salad, chicken, and pancakes keys would the values instead.

$meals = array(
    'Lunch' => array(
            'salad' => 'italian',
            'salad2' => 'ranch',
            'salad3' => 'sesame',
            'salad4' => 'bluecheese'
        ), 

    'Dinner' => array(
            'chicken' => 'grilled',
            'chicken2' => 'baked',
            'chicken3' => 'steamed',
            'chicken4' => 'fried',
            'chicken5' => 'broiled'
        ), 

    'Breakfast' => array(
        'pancakes' => 'blueberry',
        'pancakes2' => 'cherry',
        'pancakes3' => 'strawberry',
        'pancakes4' => 'lemon'
        )
);

$newkey = array();

foreach($meals as $key => $value) {
    unset($value);
    // foreach ($)...
}


print_r($meals);
0

4 Answers 4

1

Edit

If you're unable to use array_flip(), then you'll need to do two loops: one for each meal, and one for each meal option.

Example:

foreach ($meals as $meal => $mealOptions)
{
    $revisedMealOptions = array();

    foreach ($mealOptions as $originalKey => $newKey)
    {
        $revisedMealOptions[$newKey] = $originalKey;
    }

    $meals[$meal] = $revisedMealOptions;
}

Original Answer

It's not entirely clear what you're after. If all you want is to turn:

'Lunch' => array(
    'salad' => 'italian'
);

into

'Lunch' => array(
    'italian' => 'salad'
);

use array_flip().

Example:

$meals['Lunch'] = array_flip($meals['Lunch']);

See http://php.net/manual/en/function.array-flip.php.

Sign up to request clarification or add additional context in comments.

2 Comments

what if you had two values that were the same? For example, 'salad' and 'salad2' both have values 'italian.' How would you ensure these still get printed out? $meals = array( 'Lunch' => array( 'salad' => 'italian', 'salad2' => 'italian', 'salad3' => 'sesame', 'salad4' => 'bluecheese' ),
@susanmanchester if you have two values for the same key, you'll need to find an alternative structure, since array keys are unique. The easiest way is to use isset or array_key_exists to determine if the key you're trying to provide a value for exists, and if it does, store each value inside another internal array. Something like if (isset($revisedMealOptions[$newKey])) { if (is_array($revisedMealOptions[$newKey])) { $revisedMealOptions[$newKey][] = $originalKey; } else { $revisedMealOptions[$newKey] = array($revisedMealOptions[$newKey], $originalKey); } }
0

I'm sorry I am a total beginner. I need to use a nested $foreach loop to address this question. For class I'm not allowed to use the array_flip function. I have tried

$newkey = array();

foreach($meals as $key => $value) {
  $newkey[] = $value; 
     foreach ($value as ){
    }
  }

1 Comment

You should edit this into your original question; don't make answers.
0

So, it would read:

Basically I need it to read:

Array(

[Lunch] => array(
        [italian] => salad,
        [ranch] => salad2,
        [sesame] => salad3,
        [bluecheese] => salad4
    )
 )

Comments

0

The easiest way to do it would be like this:

$meals = array(
    'Lunch' => array(
        'salad' => 'italian',
        'salad2' => 'ranch',
        'salad3' => 'sesame',
        'salad4' => 'bluecheese'
    ),

    'Dinner' => array(
        'chicken' => 'grilled',
        'chicken2' => 'baked',
        'chicken3' => 'steamed',
        'chicken4' => 'fried',
        'chicken5' => 'broiled'
    ),

    'Breakfast' => array(
        'pancakes' => 'blueberry',
        'pancakes2' => 'cherry',
        'pancakes3' => 'strawberry',
        'pancakes4' => 'lemon'
    )
);

$newArray = array();

foreach ($meals as $key => $value) {
    $temp = array();
    foreach ($value as $innerKey => $innerValue) {
        $temp[$innerValue] = $innerKey;
    }
    $newArray[$key] = $temp;
}

unset($meals);

Basically, you create a new array and build it from the beginning with the use of the array $meals.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.