1
<?php
    $a = $monitoring->getMonitoringServers();
    if (in_array("192.168.100.253", $a))
        echo "y";
    else
        echo "n";
?>

print_r($a) yields:

Array (
   [0] => stdClass Object ( [address] => 192.168.100.253 )
   [1] => stdClass Object ( [address] => 192.168.100.253 )
) 

What's wrong with this code? The answer is always no!

1
  • 1
    Please improve the question title. It doesn't really say anything. Commented Jun 8, 2011 at 11:12

4 Answers 4

2

Your array is made of objects, not strings.

By doing in_array("192.168.100.253", $a) you are looking for the string "192.168.100.253" inside $a, and as you can see on the print_r - it's inside an object.

Sign up to request clarification or add additional context in comments.

Comments

1
$flag = false;
foreach($a as $obj){
  if($obj->address == "192.168.100.253"){
     $flag = true;
     break;
  }
}

if($flag){
   echo 'Y';
}
else{
   echo 'N';
}

$a is array of stdObjects, and you are treating them as normal values.

You are required to use a foreach loop to iterate through each element of $a.

Comments

0

The $a var is an object, not an array.

See http://www.php.net/manual/en/function.in-array.php#103983 for a function that also works on objects.

1 Comment

No, it's an array of objects.
0

its simple try dis

$a = $monitoring->getMonitoringServers();

if (!in_array("192.168.100.253", $a->address ) echo "y"; else echo "n"; ?> u l get output as n

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.