Jump to content


Photo

In_array Function...wrong Datatype?


  • Please log in to reply
3 replies to this topic

#1 alexruimy

alexruimy
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 28 June 2006 - 12:05 PM

I'm sure there's something wrong here on a basic level that I'm just unaware of. Here're a few lines of code that aren't functioning properly.

The error I'm getting is
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: in_array(): Wrong datatype for second argument in...[/quote]

The relevant code is:

$check = array(
       "service" => "Service",
       "centrifuge" => "Centrifuge",
       "support" => "Support",
       "disinfection" => "Disinfection",
       "main" => "Main",
       "northcontrol" => "North Control");

function echotitle($b){
   if (in_array($b, $check)) {
      echo " - ". ($check[$b]) . " Building";
      } else {
      echo "Overview";}
}

I'm aware this may not be the most efficient way to make what I'm trying to make happen, but I'm stuck on this now, and really want to make it work. I just learned the "=>" thing in arrays, and I'm trying to put it to use. My guess is that that's where the problem lies.

Any thoughts?

#2 alexruimy

alexruimy
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 28 June 2006 - 01:39 PM

Gaah, I can't figure this damn thing out.

I found this at php.net, but can't figure out how to apply it:

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Sometimes, you might want to search values in array, that does not exist. In this case php will display nasty warning:
Wrong datatype for second argument in call to in_array() .

In this case, add a simple statement before the in_array function:

if (sizeof($arr_to_searchin) == 0 || !in_array($value, $arr_to_searchin)) { ... }

In this case, the 1st statement will return true, omitting the 2nd one[/quote]

#3 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 28 June 2006 - 02:00 PM

function echotitle($b){
   
   global $check;

   if (sizeof($arr_to_searchin) == 0 || !in_array($value, $arr_to_searchin)) {
      if (in_array($b, $check)) {
         echo " - ". ($check[$b]) . " Building";
         } else {
         echo "Overview";}
   }
}

I've also added global $check because it was not in the scope.
[a href=\"http://www.php.net/static\" target=\"_blank\"]http://www.php.net/static[/a]
~ D Kuang

#4 alexruimy

alexruimy
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 28 June 2006 - 02:08 PM

Brilliant! Thanks much.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users