0

我正在尝试做一个网站(不要问它是什么),我需要帮助。我创建了这个名为 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));

?>

你明白了。我在 index.php 上创建了一个 foreach 循环,它创建了一个选择:

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

它发布 selectMeal[] 并将其放入 searchmeal.php 中的 $search 中。我想要的是,我希望它搜索 meallist.php 中的 $yemekaralist 并检查 ing1 (如果可用,则 ing2 )是否与 selectMeal[] 数组匹配,如果可用则打印 name => "something"。我怎样才能做到这一点?

最好的问候,mission712

4

1 回答 1

0

你能改变 $yemekaralist 的结构吗?

如果你改变这样的结构,它会简化你的算法:

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

然后像这样:

foreach ($yemekaralist as $item) {
    foreach ($item['ingredients'] as $ingredient) {
        foreach ($selectMeal as $selectedMeal) {
            if ($selectedMeal == $ingredient) {
                echo $item['name'];
            }
        }
    }
}
于 2013-10-05T11:20:04.640 回答