如果条件不适用于 Foreach 语句 PHP?

If Condition Doesnot Work on Foreach Statement PHP?

所以我想在 if 语句与条件匹配时制作动态 Fontawesome 图标

foreach ($facility as $facility  => $fac) {
     echo  '<div class="col-span-4">';
     echo  '<div class="flex w-full">';
     if ($fac == 'Air Conditioner') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-fan"></i></div>';
     } else if ($fac == 'Cable Tv') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-tv"></i></div>';
     } else if ($fac == 'Dining Area') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-utensils"></i></div>';
     } else if ($fac == 'Electricity') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-plug"></i></div>';
     } else if ($fac == 'Garage') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-warehouse"></i></div>';
     } else if ($fac == 'Gazebo') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-home"></i></div>';
     } else if ($fac == 'Internet') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-globe"></i></div>';
     } else if ($fac == 'Kitchen') {
        echo '<div class="w-8 text-bvr20"><i class="far fa-hat-chef"></i></div>';
     } else if ($fac == 'Level') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-level-up-alt"></i></div>';
     } else if ($fac == 'Pool') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-swimmer"></i></div>';
     } else if ($fac == 'Security') {
        echo '<div class="w-8 text-bvr20"><i class="fad fa-shield"></i></div>';
     } else if ($fac == 'Storage') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-box"></i></div>';
     } else if ($fac == 'Washer or Laundry') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-washer"></i></div>';
     } else if ($fac == 'Water Resource') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-water"></i></div>';
     } else if ($fac == 'Wi-Fi') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-wifi"></i></div>';
     }
     echo  '<p>' . $fac . '</p>';
     echo  '</div>';
     echo  '</div>';
}

显示变量值的底部回显正确显示了$facility 的可用数组项,但该条件不起作用,它只起作用一次,仅限空调。有人知道我哪里出了问题吗?

你可以像那样使用 foreach

foreach (iterable_expression as $value){
//commands
}

foreach (iterable_expression as $key => $value){
    //commandes
}

试试这个:

foreach ($facilityList as $facility  => $fac) {
     echo  '<div class="col-span-4">';
     echo  '<div class="flex w-full">';
     if ($fac == 'Air Conditioner') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-fan"></i></div>';
     } else if ($fac == 'Cable Tv') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-tv"></i></div>';
     } else if ($fac == 'Dining Area') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-utensils"></i></div>';
     } else if ($fac == 'Electricity') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-plug"></i></div>';
     } else if ($fac == 'Garage') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-warehouse"></i></div>';
     } else if ($fac == 'Gazebo') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-home"></i></div>';
     } else if ($fac == 'Internet') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-globe"></i></div>';
     } else if ($fac == 'Kitchen') {
        echo '<div class="w-8 text-bvr20"><i class="far fa-hat-chef"></i></div>';
     } else if ($fac == 'Level') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-level-up-alt"></i></div>';
     } else if ($fac == 'Pool') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-swimmer"></i></div>';
     } else if ($fac == 'Security') {
        echo '<div class="w-8 text-bvr20"><i class="fad fa-shield"></i></div>';
     } else if ($fac == 'Storage') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-box"></i></div>';
     } else if ($fac == 'Washer or Laundry') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-washer"></i></div>';
     } else if ($fac == 'Water Resource') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-water"></i></div>';
     } else if ($fac == 'Wi-Fi') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-wifi"></i></div>';
     }
     echo  '<p>' . $fac . '</p>';
     echo  '</div>';
     echo  '</div>';
}

你有很多不必要的重复代码。让 PHP 为您完成这项工作。考虑一下,如果每一行都相同,除了一个小部分,你只需要重复那个小部分。

<?php
//create a key-value pair so you don't have to have an IF statement for each condition
$facility_map = [
    'air_conditioner' => 'fan',
    'cable_tv' => 'tv',
    'dining_area' => 'utensils',
    'electricity' => 'plug',
    'garage' => 'warehouse',
    'gazebo' => 'home',
    'internet' => 'globe',
    'kitchen' => 'chef',
    'level' => 'level-up-alt',
    'pool' => 'swimmer',
    'security' => 'shield',
    'storage' => 'box',
    'washer_or_laundry' => 'washer',
    'water_resource' => 'water',
    'wi-fi' => 'wifi'
];

foreach($facility as $fac) {

    //trim whitespae at the beginning and end, lowercase everything, change spaced to underscores. Easier to look up in array
    $fac_cleaned = str_replace(' ', '_', trim(strtolower($fac)));

    //check if key in map exists? If not, print and move to next key
    if(!array_key_exists($fac_cleaned, $facility_map)) {
        echo "Unhandled Key ({$fac_cleaned})<br>";
        continue;
    }

    //grab the value from the $facility_map array
    $icon = $facility_map[$fac_cleaned];

    ?>
        <div class="col-span-4">
            <div class="flex w-full">
                <div class="w-8 text-bvr20"><i class="far fa-<?= $icon ?>"></i></div>
                <p><?= $fac ?></p>
            </div>
        </div>
    <?php
}

这种方法有一个很大的优点,就是很容易扩展。您可以向 $facility_map 数组添加更多值,或者您甚至可以从数据库生成该数组。

这对我有用....

<?php
$facilityList = [
    'air_conditioner' => 'fan',
    'cable_tv' => 'tv',
    'dining_area' => 'utensils',
    'electricity' => 'plug',
    'garage' => 'warehouse',
    'gazebo' => 'home',
    'internet' => 'globe',
    'kitchen' => 'chef',
    'level' => 'level-up-alt',
    'pool' => 'swimmer',
    'security' => 'shield',
    'storage' => 'box',
    'washer_or_laundry' => 'washer',
    'water_resource' => 'water',
    'wi-fi' => 'wifi'
];


foreach ($facilityList as $facility  => $fac) {
     echo  '<div class="col-span-4">';
     echo  '<div class="flex w-full">';
     if ($fac == 'Air Conditioner') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-fan"></i></div>';
     } else if ($fac == 'Cable Tv') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-tv"></i></div>';
     } else if ($fac == 'Dining Area') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-utensils"></i></div>';
     } else if ($fac == 'Electricity') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-plug"></i></div>';
     } else if ($fac == 'Garage') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-warehouse"></i></div>';
     } else if ($fac == 'Gazebo') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-home"></i></div>';
     } else if ($fac == 'Internet') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-globe"></i></div>';
     } else if ($fac == 'Kitchen') {
        echo '<div class="w-8 text-bvr20"><i class="far fa-hat-chef"></i></div>';
     } else if ($fac == 'Level') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-level-up-alt"></i></div>';
     } else if ($fac == 'Pool') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-swimmer"></i></div>';
     } else if ($fac == 'Security') {
        echo '<div class="w-8 text-bvr20"><i class="fad fa-shield"></i></div>';
     } else if ($fac == 'Storage') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-box"></i></div>';
     } else if ($fac == 'Washer or Laundry') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-washer"></i></div>';
     } else if ($fac == 'Water Resource') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-water"></i></div>';
     } else if ($fac == 'Wi-Fi') {
        echo '<div class="w-8 text-bvr20"><i class="fas fa-wifi"></i></div>';
     }
     echo  '<p>' . $fac . '</p>';
     echo  '</div>';
     echo  '</div>';
}

?>