dongque1646 2012-03-16 14:38
浏览 37
已采纳

使用foreach修复/改进if语句不为null

Having this code:

$main = !empty($searchResults['main']) ? $searchResults['main'] : null;
$second = !empty($searchResults['second']) ? $searchResults['second'] : null;
$third = !empty($searchResults['third']) ? $searchResults['third'] : null;

#if(($main) || ($second) || ($third))
if((($main) || ($second) || ($third)) !== NULL)
{
    foreach ((array)$searchResults as $key => $value)
    {
        switch ($key)
        {
            case "main":
            ....

What can it be done to fix/improve this code?

By fix I mean that I need a way to avoid running switch on empty keys

  • 写回答

2条回答 默认 最新

  • dongyejun1983 2012-03-16 14:46
    关注
    foreach ($searchResults as $key => $value)
    {
      if(empty($value)) continue;
      ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?