6

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.

2
  • in_array works for me Commented May 25, 2013 at 14:04
  • 2
    This isn't a duplicate question -- this person's problem is mistaking the value 0 for FALSE. The OP's problem in the "duplicate question" cited is mistaking arrays for comma-separated strings. Commented May 14, 2015 at 22:51

1 Answer 1

23

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

2 Comments

but why in_array() fails?
i don't see in_array in your example but see eval.in/31491

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.