Jump to content

Recommended Posts

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:

[code]$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";}
}[/code]

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?
Link to comment
https://forums.phpfreaks.com/topic/13098-in_array-functionwrong-datatype/
Share on other sites

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]
[code]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";}
   }
}[/code]

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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.