Seba99
Seba99

Reputation: 1309

PHP Array_search won't find an existing value

I'm trying to use array_search on a simple array :

        echo "<br> Search for : -".ucfirst(strtolower(trim($rowData[0][2]))).'-';

here is the index of the value ( I know it but I want PHP to find it for me >< ):

        echo '<br>-'.$listMetiers[0].'-'; 

Here is the full array :

        echo '<pre>';
        print_r($listMetiers);
        echo '</pre>';

        $id_metier = array_search(ucfirst(strtolower(trim($rowData[0][2]))),$listMetiers);

        if(!$id_metier)
        {
          echo ' NOT FOUND !<br>';
          $id_metier = -666;
        }
        else
        {
          echo 'GOOD : '.$id_metier.'<br>';
        }

The value is in the array but array_search don't find it ! Look what I have when executing this code :

enter image description here

What is going on ?

Upvotes: 0

Views: 162

Answers (2)

Nenad Mitic
Nenad Mitic

Reputation: 567

array_searh is returning 0, and PHP is treating that as a falsy value.

You should change this

!$id_metier

into this

$id_metier === false

Upvotes: 3

Ghilas BELHADJ
Ghilas BELHADJ

Reputation: 14106

Your test is wong, use:

if($id_metier < 0){ // not found
// ...
} else {            // found
// ...
}

Upvotes: 1

Related Questions