gepex
gepex

Reputation: 197

php solution too huge if statement

Is there a way that I can approach this to some other solution?

I have 7 arrays (monday, tuesday, wednesday, thrusday, friday, saturday, sunday). Each one of this arrays have arrays inside of them. and I need to check if all days have the same amount of data.

So im comparinng something like this:

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
  }

As you can see it would become a huge if statement tree, I dont have that much of experience to know any other approach to this, but id you do, please help me!

thank you!

Upvotes: 0

Views: 109

Answers (4)

Steven Schmutz
Steven Schmutz

Reputation: 19

This solution uses the fact that the two comparison variables are quite predictable (ie. a single day is compared with the very next day).

All the daily arrays are placed into a single weekly array, which is then looped over. If there is a discrepancy, the contents of the last variable are displayed and the loop continues.

If there are no discrepancies then the success message is displayed.

<?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";   
}

Upvotes: 0

Hameed
Hameed

Reputation: 2277

How about this:

<?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>";
?>

Initial array of days:

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
        )

)

and Output:

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

Upvotes: 0

user1844933
user1844933

Reputation: 3417

$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';
}

Upvotes: 0

NewInTheBusiness
NewInTheBusiness

Reputation: 1475

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

$diff will contain the differences if there are any...

Upvotes: 4

Related Questions