2

I am not finding what might be the problem in my program.

I am having an array like this

$arr = array("1", "urgent", "4", "low", "15", "avg");

When i am searching this array using

$key = array_search("4", $arr);// Working

Its is giving me the index of that element;

But when i am searching for "1" it is not giving me any index.

$key = array_search("4", $arr); // Not working here - for searching "1"

What might be the problem.

Thank You

15
  • You might want to fix that second example. Also, what is it giving you? How do you determine it's "not working"? Commented Jan 22, 2011 at 6:31
  • 2
    There are so many duplicates, plus the documentation explicitly covers this case. With a huge red box. Commented Jan 22, 2011 at 6:38
  • 1
    @zerkms That would be ideal, in my opinion. The extremely trivial stuff doesn't belong here. Commented Jan 22, 2011 at 6:47
  • 1
    @meagar: What defines "extremely trivial"? Not everybody on SO is a guru, that's why the site exists in the first place. Sure, it's written in the documentation, but the fact that searching for "problem with array search php" (a query for which most having the same problem will actually use) ranks first in Google, even before the manual, says it all. SO is a reference, even if the answer is "check the manual here and there", it's still useful and for that reason this kind of question belongs on SO in my opinion. At least in this case OP tried, unlike many others who go "will this work?". Commented Jan 22, 2011 at 6:56
  • 1
    @netcoder There is no explicit rule saying that "How do I add two numbers in C++?" is a bad question, but it's still a bad question. There has to be a line where questions are too trivial for us to bother copy-pasting the documentation. All this aside, my bigger beef with this question is that it's a duplicate of a duplicate of a duplicate. Commented Jan 22, 2011 at 7:39

4 Answers 4

5

http://ideone.com/e1n3r Your code does work fine. It returns key == 0, which is the key, where "1" is stored.

To get the difference between 0 and false you should use === operator, or its contrary !==:

$arr = array("1", "urgent", "4", "low", "15", "avg");

$key1 = array_search("1", $arr); 
var_dump($key1 === false);  // false (value exists)
var_dump($key1 !== false);  // true (value exists)

$key21 = array_search("21", $arr); 
var_dump($key21 === false); // true  (value does not exist)
var_dump($key21 !== false); // false (value does not exist)
Sign up to request clarification or add additional context in comments.

Comments

0

array_search return key value of array.

    $arr = array("1", "urgent", "4", "low", "15", "avg");
    $key = array_search("4", $arr); 
   // give output is 2 which is key value 
    $key = array_search("1", $arr);
   //give output is 0 which is key value

Comments

0

I think you are using $key in thewrong way. $key = 0 and now if you do boolean evaluation it will act as false. You should use something like if($key >= 0) or if(is_int($key))

Comments

0

Remember that 0 is evaluated to FALSE in php.

so..

if($index = array_search("1",$arr)) {
    //will actually evaluate to false
}

In case you are using it this way and think it's not working because the conditional isn't technically failing, the value is evaluated as FALSE.

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.