Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I am using a simple php script to look for an element in an array like

    $restricted = array('root/base', 'root2' ); 
    print_r($restricted);
    if( array_search('root/base', $restricted) ){
        echo "1";
    } else {
        echo "0";
    }

But I am always getting the following output

Array ( [0] => root/base [1] => root2 ) 0

This means that the array_search is failing to find the element in the given array. Can anybody show some light on whats happening?

I tried to replace array_search() with in_array() also. But that too returned the same error.

share|improve this question
 
in_array works for me –  Tamil Selvan May 25 at 14:04
add comment

marked as duplicate by likeitlikeit, Stony, Cairnarvon, Rubens, Undo May 26 at 0:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 7 down vote accepted

From PHP DOC

array_search — Searches the array for a given value and returns the corresponding key if successful

The index is 0 that is why you think its fails

Use

array_search('root/base', $restricted) !== false
share|improve this answer
 
but why in_array() fails? –  rahul Ram May 25 at 14:04
1  
i don't see in_array in your example but see eval.in/31491 –  Baba May 25 at 14:05
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.