1

I need to check the checkbox when the in_array function is true. The looping going fine but it checks all checkboxes whether the bool is true or false.

$pixArr

Array(
    [12] => Array
        (
            [imgFile] => IMG_7516.JPG
            [imgTime] => 11:39
        )

    [13] => Array
        (
            [imgFile] => IMG_7515.JPG
            [imgTime] => 11:39
        )

)

$dTime

Array(
    [0] => 11-26-50
    [1] => 11-26-50
    [2] => 11-39-43
    [3] => 11-39-43
    [4] => 14-35-38
)

$fTime=11-26-50

foreach($pixArr as $key=>$val){
    if(in_array($fTime,$dTime)){
    echo "<input type=\"checkbox\" name=\"file[]\" value=\"$val[imgFile]\" checked/>&nbsp;Select</label>";
    }else{
    echo "<input type=\"checkbox\" name=\"file[]\" value=\"$val[imgFile]\"/>&nbsp;Select</label>";
    }
}

I expect the input which time 11-26-50 is checked. Because is turns out the in_array true. But it checks every input in the loop. I don't understand why.

3
  • 1
    This is because the value is in the array everytime, regardless of what we are looping over Commented Feb 9, 2016 at 7:31
  • 1
    codeHeart is right. you iterate over $pixArr, but only check $fTime and $dTime, which do not change. i guess you'd have to check $val['imgTime'] instead? but there you have to adjust the used time format Commented Feb 9, 2016 at 7:33
  • I think it would be easier to give an answer if you give a little background about the problem, right now we don't have an idea of what is dtime or ftime and what do you want to achieve. As Mentioned by Franz, that too can be a solution Commented Feb 9, 2016 at 7:36

4 Answers 4

1

This is what you need to check as problem described by you

foreach($dTime as $key=>$val)
{    
    if($fTime == $val)
    {
        echo "<input type=\"checkbox\" name=\"file[]\" value=\"$val[imgFile]\" checked/>&nbsp;Select</label>";
    }
    else
    {
        echo "<input type=\"checkbox\" name=\"file[]\" value=\"$val[imgFile]\"/>&nbsp;Select</label>";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

To compare 'time' values as strings they should be brought into the same format:

$dTime = [
    0 => '11-26-50',
    1 => '11-26-50',
    2 => '11-39-43',
    3 => '11-39-43',
    4 => '14-35-38'
];

$dTimeFormatted = array_map(function($v){
    return substr(str_replace("-",":",$v), 0, 5);
}, $dTime); 

foreach ($pixArr as $key => $val) {
    $inTime = in_array($val['imgTime'], $dTimeFormatted);        
    echo "<input type=\"checkbox\" name=\"file[]\" value=\"{$val['imgFile']}\" ".(($inTime)? '"checked"':" " )."/>&nbsp;Select</label>";        
}

1 Comment

This is the new trick of replace a string in array (of me). Thank you very much.
0

You need to change $fTime within your foreach-loop. If not, you're just going to loop through the full $pixArr since in_array($fTime,$dTime) is true all the time.

foreach($pixArr as $key=>$val){
    if(in_array($fTime,$dTime)){
    echo "<input type=\"checkbox\" name=\"file[]\" value=\"$val[imgFile]\" checked/>&nbsp;Select</label>";
    }else{
    echo "<input type=\"checkbox\" name=\"file[]\" value=\"$val[imgFile]\"/>&nbsp;Select</label>";
    }
    $fTime = //update fTime here
}

Comments

0

I knew now. Because the compare value in in_array is nothing to do with the loop. I cannot compare $fTime with $dTime. I have to compare the $val[imgTime] with $dTime in the same format.

Here is the point. Now $dTime format is HH-mm-ss while $val[imgTime] is HH:mm:ss.

I have to change them into the same format. So I decided to change $val[imgTime].

$vTime=str_replace(":","-",$val['imgTime']);

So, the final script is:

foreach($pixArr as $key=>$val){
$vTime=str_replace(":","-",$val['imgTime']);
if(in_array($vTime,$dTime)){
    echo "<input type=\"checkbox\" name=\"file[]\" value=\"$val[imgFile]\" checked/>&nbsp;Select</label>";
}else{
    echo "<input type=\"checkbox\" name=\"file[]\" value=\"$val[imgFile]\"/>&nbsp;Select</label>";
}

Thanks for the clues from @codeHeart, @frank and every answers.

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.