php解决方案如果声明太大了

时间:2014-01-09 06:12:25

标签: php if-statement

有没有办法可以解决这个问题呢?

我有7个阵列(星期一,星期二,星期三,星期五,星期五,星期六,星期日)。 这些数组中的每一个都在其中包含数组。我需要检查所有天数是否都有相同的数据。

所以即便如此:

if(count($monday) == count($tuesday)){
  if(count($tuesday) == count($wednesday)){
     if(count($wednesday) == count($thursday)){
    if(count($thursday) == count($fruday)){
       if(count($fruday) == count($saturday)){
         if(count($saturday) == count($sunday)){
                echo 'ok whole week is the same';
             }
             else{
               //print sunday
               //compare the rest which could print and keep comparing rest of the days
             }
           }
       else{
             //print saturday and comapre
           }
          }
        else{
         //print friday and compare
        }
       }
       else{
        //print thurdasy and compare
       }
      }
      else{
        //print wednesday and compare
      }
     }
     else{
       //print tuesday and compare
     }
    }
    else{
      //print monday
      //compare rest of the days
  }

正如你所看到的,它将成为一个巨大的if语句树,我没有那么多经验知道任何其他方法,但你知道,请帮助我!

谢谢你!

4 个答案:

答案 0 :(得分:4)

使用$diff = array_diff($monday,$tuesday,$wednesday, etc...);

$diff将包含差异,如果有...

答案 1 :(得分:0)

$bol = 0;

if(count($monday) == count($tuesday)){
$bol = $bol+1
} else {print $moday;}

//continue for all weeks

if ($bol ==6) {
echo 'ok whole week is the same';
}

答案 2 :(得分:0)

这个怎么样:

<?php
echo "<pre>";
$days = array('sunday' => array(1,2), 'saturday' => array(1,2,3), 'friday' => array(1,2,3,5), 'thursday' => array(1,2,3), 'wednesday' => array(1,2,3,4,5,6), 'tuesday' => array(1), 'monday' => array(1,2, 3,4,5,6,7,8));
//Create a count array
$count = array();

//populate count array
foreach ($days as $k => $v)
{
    $count[$k] = count($v);
}

//reverse sort it
arsort($count);

//if both end of array elements are the same all are the same
if (reset ($count) == end($count)) 
{
    echo 'ok whole week is the same';
} else {
    foreach ($count as $d => $c)
    {
        echo $d . ": " . implode ( ", " , $days[$d] ) . "\n";
    }
}

echo "</pre>";
?>

最初的天数:

Array
(
    [sunday] => Array
        (
            [0] => 1
            [1] => 2
        )

    [saturday] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [friday] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 5
        )

    [thursday] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [wednesday] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
        )

    [tuesday] => Array
        (
            [0] => 1
        )

    [monday] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
            [5] => 6
            [6] => 7
            [7] => 8
        )

)

和输出:

monday: 1, 2, 3, 4, 5, 6, 7, 8
wednesday: 1, 2, 3, 4, 5, 6
friday: 1, 2, 3, 5
thursday: 1, 2, 3
saturday: 1, 2, 3
sunday: 1, 2
tuesday: 1

答案 3 :(得分:0)

该解决方案使用两个比较变量非常可预测的事实(即,将第一天与第二天进行比较)。

所有每日数组都放在一个单独的每周数组中,然后循环遍历。如果存在差异,则显示最后一个变量的内容并继续循环。

如果没有差异,则显示成功消息。

<?php
$bIsCorrect = true;
$weeks_array = array($monday,$tuesday,$wednesday,$thursday,$friday,$saturday,$sunday);

//Loop through weeks starting from $monday and going only to $saturday
for($i=0,$max_count=6; $i<$max_count; $i++) {
    if(count($weeks_array[$i]) != count($weeks_array[$i+1])) {
        $bIsCorrect = false;
        $array_text = var_export($weeks_array[$i+1], true);
        echo "Discrepancy : <br> $array_text";
        echo "<hr>";
    }
}

if($bIsCorrect) {
    echo "ok whole week is the same";   
}