0

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 ?

2
  • 1
    Your found index is 0 and !0 is == TRUE, so it enters the if statement; Check if $id_metier is equals FALSE or not Commented Oct 17, 2015 at 15:04
  • OMG thanks ! Haha why I didn't saw that ! Commented Oct 17, 2015 at 15:07

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

correct, but the op wants to check if it doesn't exist, so === false
Yup @Rizier123 commented on this ! :) That's it but better change into $id_metier === false ;)
1

Your test is wong, use:

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

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.