Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

array search 1

Status
Not open for further replies.

MrBelfry

IS-IT--Management
May 21, 2003
289
Hey there

when using the array_search function I am running into problems. I want to delete an element from an array if it is set to a certain value. But if the key that array_search returns is 0 then PHP also thinks that means false! Which means I either don't delete the element thinking it isn't in the array OR I delete the first element even if that wasn't what I was searching for.

How can I tell if hte 0 i return is a value of false or if it is an array key?

MrBelfry
 
I'm not sure I completely understand your problem. I think you are unable to tell the difference between the first (0) element of the array and a false return code. If that is the case, simply don't use that element by giving it a key you never search for (like -1 or -999999). Then you know a 0 return means it did not find the element you were looking for.
 
You're able to tell the difference between false and zero if you use the === operator because === also compare var types.

Consider this example:

Code:
<?php

  function doTest($input) {
    if($input===0)
      echo "It's a zero!<BR>";
    if($input==="0")
      echo "It's a char!<BR>";
    if($input=== false)
      echo "It's false!<BR>";
  }

  $test1 = false;
  doTest($test1);
  echo "<HR>";

  $test2 = "0";
  doTest($test2);
  echo "<HR>";

  $test3 = 0;
  doTest($test3);
  echo "<HR>";

?>

Hope this can help you ...


Jakob §:O)
 
I didn't know that! This is what I like about this forum. There is always new things to learn and I didn't even ask this question. Thanks for the tip.
 
You're welcome -and absolutely right -there is always new things to learn §;O)

Did this solve your problem?


Jakob §:O)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top