Skip to content

array_search() DOES NOT check anything in position 0 #8775

@sujyrokimora

Description

@sujyrokimora

Description

The following code:

$array1=['289','290','297'];
$array2=['289','290','297'];
$num=0;
while ($num<count($array1))
{
    echo print_r($array1,true);
    echo '<br>';
    echo print_r($array2,true);
    $t=$array1[$num];
    echo '<br>';
    echo  $t;
    echo '<br>';
     if (array_search($t,$array2,TRUE))
     {
         echo 'This matched: '.$array1[$num];
     }
    echo '<br>';
    $num++;
}

Resulted in this output:

Array ( [0] => 289 [1] => 290 [2] => 297 )
Array ( [0] => 289 [1] => 290 [2] => 297 )
289
Array ( [0] => 289 [1] => 290 [2] => 297 )
Array ( [0] => 289 [1] => 290 [2] => 297 )
290
IThis matched: 290
Array ( [0] => 289 [1] => 290 [2] => 297 )
Array ( [0] => 289 [1] => 290 [2] => 297 )
297
This matched: 297

But I expected this output instead:

Array ( [0] => 289 [1] => 290 [2] => 297 )
Array ( [0] => 289 [1] => 290 [2] => 297 )
289
This matched: 289
Array ( [0] => 289 [1] => 290 [2] => 297 )
Array ( [0] => 289 [1] => 290 [2] => 297 )
290
IThis matched: 290
Array ( [0] => 289 [1] => 290 [2] => 297 )
Array ( [0] => 289 [1] => 290 [2] => 297 )
297
This matched: 297

https://3v4l.org/r5tN9#v8.1.7

PHP Version

PHP 8.1.2

Operating System

Windows 10- 10.0.19044 N/A Build 19044

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions