简体   繁体   English

使用PHP缩短if else语句

[英]Shorten if else statement using php

i have a question about shorten if else statement. 我有一个关于缩短if else语句的问题。 I am trying to make a weather application using OpenWeatherMap api. 我正在尝试使用OpenWeatherMap API创建天气应用程序。 But i don't like those icons. 但是我不喜欢那些图标。 I want to change the icons like this: 我想这样更改图标:

if($desc == 'clear sky'){
  $weather_icon = 'clear_sky.png';
}else
if($desc == 'few clouds'){
  $weather_icon = 'few_clouds.png';
}else
if($desc == 'scattered clouds'){
  $weather_icon = 'scattered_clouds.png';
}else
if($desc == 'broken clouds'){
  $weather_icon = 'broken_clouds.png';
}else
if($desc == ''){
  .....
}
......

So my question is how can i do this with shorten if else or do you have any idea to use different thinks? 所以我的问题是,如果不这样做,或者如果您有任何想法使用不同的想法,我该如何缩短呢?

Since your description matches what you're looking for you could do this. 由于您的描述与您要查找的内容匹配,因此可以执行此操作。

if (
    in_array(
        $desc,
        array(
            'clear sky',
            'few clouds',
            'scattered clouds',
            'broken clouds'
        )
    )
) {
    $weather_icon = str_replace(' ', '_', $desc) . '.png';
}

Another option would be to use a map, they they don't always match. 另一种选择是使用地图,它们并不总是匹配的。

$map = [
    'clear sky' => 'clear_sky.png',
    'few clouds' => 'few_clouds.png',
    'scattered clouds' => 'scattered_clouds.png',
    'broken clouds' => 'broken_clouds.png',
    'thunderstorm with light rain' => 'few_clouds.png',
];

// $api['icon'] references the original icon from the api
$weather_icon = array_key_exists($desc, $map) ? $map[$desc] : $api['icon'];

Arrays are the glue that holds the universe together (if the universe is written in PHP). 数组是将Universe保持在一起的胶水(如果Universe是用PHP编写的)。

$map = [
   'clear sky' => "clear_sky.png",
   'few clouds' =>"few_clouds.png", 
   'scattered clouds' => 'scattered_clouds.png'
   'broken clouds' => 'broken_clouds.png'
];

if (isset($map[$desc])) {
   $weather_icon = $map[$desc];
} 

This allows you to also map unrelated words with image names as well as multiple words to the same image. 这样还可以将不相关的单词与图像名称以及多个单词映射到同一图像。

If your weather patterns are predictable, you can just use a one liner: 如果您的天气状况是可以预测的,则可以只使用一种衬垫:

$img = str_replace ( ' ' , '_', $desc ) . '.png';

However if you have a list that you cannot just alter dynaically you can use this: 但是,如果您有一个列表,您不能仅进行动态更改,则可以使用以下命令:

$descriptions = [
     'clear sky'=>'clear_sky',
     'few clouds'=>'few_clouds',
     'scattered clouds'=>'scattered_clouds',    
     'broken clouds'=>'broken_clouds',    
];

$defaultImg = 'some_empty';

$img = !empty($desc) ? $descriptions[$desc] : $defaultImg;
$img = $img . 'png';
<?php
$desc = "clear sky";
$weather_icon = str_replace(" ","_",$desc).".png";
echo $weather_icon;
?>

It looks like you have a some fixed notation. 看来您有一些固定的符号。 You could use this: 您可以使用此:

<?php
$desc = 'clear sky';
convertDescriptionToImage( $desc );

function convertDescriptionToImage( $description )
{
    $arrayCodes = ["clear sky", "few clouds"];
    if ( TRUE == in_array( $description, $arrayCodes ) )
    {
        return str_replace( " ", "_", "$description.png" );
    }

    die( "$description not found" );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM