1

I am using PHP to create looping with condition, but the result was not like what I want.

Any help you can provide would be great! :)

$cnt = array(
    "08.00","09.00","10.00"
);
$time = array(
    "07.00","08.00","09.00","10.00","11.00","12.00","13.00","14.00",
    "15.00","16.00","17.00","18.00","19.00","20.00","21.00","22.00"
);

This is my looping code:

for ($i = 0; $i < 16 ; $i++) {
    for ($j = 0; $j < 3 ; $j++) {
        if ($time[$i] == $cnt[$j]) {
            $button[$i] = 'disable';
        } else {
            $button[$i] = $time[$i];
        }
    }
}

The result is:

07.00 08.00 09.00 disable 11.00 12.00 13.00 14.00 15.00 16.00 17.00 18.00 19.00 20.00 21.00 22.00

And the result i want is:

07.00 disable disable disable 11.00 12.00 13.00 14.00 15.00 16.00 17.00 18.00 19.00 20.00 21.00 22.00
1
  • I think you have many answers here. You can accept the only which you think helped you the most and solved your problem. (meta.stackexchange.com/q/5234) Commented Feb 18, 2015 at 17:05

7 Answers 7

3

I would use something like this:

$button = array();
foreach($time as $t) {
    if(in_array($t, $cnt, true)) {
        $button[]='disable';
    } else {
        $button[]=$t;
    }
}

The loop iterates through all elements of the $time array and checks for every value if the array $cnt contains the same value.

Since you are added values with incrementing indexes you can use the array push operator []= to append values to the $button array.

Learn more about in_array(): http://php.net/manual/en/function.in-array.php and []=: http://php.net/manual/en/function.array-push.php

Sign up to request clarification or add additional context in comments.

Comments

3

Your logic is flawed - you loop on the $cnt array and continually set/replace your disabled/time values. Consider this:

$time -> looking at 08:00

loop on $cnt:

08:00 -> matches -> set $button[$i] to disabled
09:00 -> no match -> set $button[$i] to $time[$i]
10:00 -> no match -> set $button[$i] to $time[$i]

Your inner loop is destructive - you only save the result of the comparison test from the LAST item in $cnt, therefore your "earlier" test results get destroyed.

What you should have is this:

foreach($time as $idx => $val) {
    if (in_array($val, $cnt)) {
        $button[$idx] = 'disabled');
    } else {
        $button[$idx] = $val;
    }
}

Comments

1

This should work for you:

(Here I go through each element of $time with array_map() and check if it is in the array of $cnt with in_array(). If it is in the array I replace it with the replacement where I use use() to inherit it from the parent scope and if not i assign the value again)

<?php

    $cnt = array("08.00", "09.00", "10.00");
    $time = array("07.00", "08.00", "09.00", "10.00", "11.00", "12.00", "13.00", "14.00", "15.00", "16.00", "17.00", "18.00", "19.00", "20.00", "21.00", "22.00");  
    $replacement = "disabled";

    $time = array_map(function ($v) use ($cnt, $replacement) {
        return (in_array($v, $cnt) ? $replacement : $v);
    }, $time);

    print_r($time);

?>

Output:

Array ( [0] => 07.00 [1] => disabled [2] => disabled [3] => disabled [4] => 11.00 [5] => 12.00 [6] => 13.00 [7] => 14.00 [8] => 15.00 [9] => 16.00 [10] => 17.00 [11] => 18.00 [12] => 19.00 [13] => 20.00 [14] => 21.00 [15] => 22.00 )

Comments

1

Lots of answers show ways of doing this, but there's also the functional approach:

$button = array_map(
    function ($item) use ($cnt) {
        return in_array($item, $cnt) ? 'disable' : $item;
    },
    $time
);

4 Comments

Not as short as your answer is are the list of errors you will get with this
@Rizier123 What do you mean?
@hek2mgl If you look at the first revision of his answer you see he didn't used global nor use(). Now magically he went to global $cnt; and then to use($cnt) i wonder where he got this from
@Rizier123 Probably you both had the same idea (a good idea) at almost the same time. Always assume good faith!
0

You might want to try the following, it's not optimal but it changes the least amount of your code while making it work:

for ($i=0; $i < 16 ; $i++) { 
     if(in_array($time[$i], $cnt) {
          $button[$i]='disable';
     } else {
          $button[$i]=$time[$i];
     }
}

Comments

0

Use PHP in_array:

for ($i=0; $i < 16 ; $i++) { 
    if(in_array( $time[$i], $cnt ){
        $button[$i]='disable';
    }else{
        $button[$i]=$time[$i];
    }   
}

Comments

-1

Just add a 'break;' in inner loop. See below,

        for ($i=0; $i < 16 ; $i++) { 
            for ($j=0; $j < 3 ; $j++) { 
                if($time[$i]==$cnt[$j]){
                    $button[$i]='disable';
                    break;
                }else{
                    $button[$i]=$time[$i];
                }   
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.