user2849211
user2849211

Reputation:

To use names when using foreach to loop multidimensional arrays?

I am trying to do a website (don't ask what it is) and I need help. I have created this page called meallist.php :

<?php
/* Ingredients List */
$meatlist = array(tavuketi => "Tavuk Eti",
                  baliketi => "Balık Eti");

$vegelist = array(domates => "Domates",
                  patates => "Patates",
                  salatalik => "Salatalık");

/* The Meals - dun dun duuuunnnnnn */

$yemekaralist = array(mangaldatavuk => array(
                                             name => "Mangalda Tavuk",
                                             ing1 => tavuketi),
                      mangaldapatateslitavuk => array(    
                                             name => "Mangalda Patatesli Tavuk",
                                             ing1 => tavuketi,
                                             ing2 => patates),
                      mangaldabalik => array(
                                             name => "Mangalda Balık",
                                             ing1 => baliketi),
                      firindapatateslibalik => array(
                                             name => "Fırında Patatesli Balık",
                                             ing1 => baliketi,
                                             ing2 => patates));

?>

You get the idea. I made a foreach loop on index.php that creates a select:

<div id="etler">
<select multiple="multiple" name="selectMeal[]" size="4" data-max-length="30">
<?php
foreach($meatlist as $id => $name)
{
    print "<option value=\"" . $id . "\">" . $name . "</option>";
}

?>
</select>
</div>
<div id="sebze">
<select multiple="multiple" name="selectMeal[]" size="4" data-max-length="30">
<?php
foreach($vegelist as $id1 => $name1)
{
    print "<option value=\"" . $id1 . "\">" . $name1 . "</option>";
}

?>
</select>
</div>

And it posts the selectMeal[] and gets it into $search in searchmeal.php. The thing I want is, I want it to search the $yemekaralist in meallist.php and check if ing1 (and ing2 if available) if it matches with the selectMeal[] array, and print name => "something" if available. How can I do that?

Best regards, mission712

Upvotes: 0

Views: 96

Answers (1)

Noquepoaqui
Noquepoaqui

Reputation: 395

Can you change the structure of $yemekaralist?

It would simplify your algorithm if you change the structure for something like this:

$yemekaralist = array(
    item1 => array(
        'name' => 'item1_name',
        'ingredients' => array('tavuketi', 'patates'),
    ),
    item2 => array(
        'name' => 'item2_name',
        'ingredients' => array('tavuketi', 'patates'),
    ),

And then go like this:

foreach ($yemekaralist as $item) {
    foreach ($item['ingredients'] as $ingredient) {
        foreach ($selectMeal as $selectedMeal) {
            if ($selectedMeal == $ingredient) {
                echo $item['name'];
            }
        }
    }
}

Upvotes: 0

Related Questions