Php in array returns false when value exists

Simple here, but it hurts your head.

echo in_array('275', $searchMe);

returns false. But if I print the array and then search it manually using my web browser, I can see that this value exists in the array.

[0] => ExtrasInfoType Object
                (
                    [Criteria] => 
                    [Code] => 275
                    [Type] => 15
                    [Name] => Pen
                )

Additional Information. The array was hidden from object to array using

$searchMe = (array) $object;

Will it be because the values ​​are not quoted? I tried using the following functions in_array:

echo in_array('275', $searchMe); // returns false
echo in_array(275, $searchMe); // returns error (Notice: Object of class Extras could not be converted to int in)

var_dump of $ searchMe

array
  'Extras' => 
    object(Extras)[6]
      public 'Extra' => 
        array
          0 => 
            object(ExtrasInfoType)[7]
              ...
          1 => 
            object(ExtrasInfoType)[17]
              ...
          2 => 
            object(ExtrasInfoType)[27]
              ...
+3
source share
2 answers

in_arraycan't see inside ExtrasInfoType Object. This is basically a comparison of ExtrasInfoType Objectto 275, which in this case returns false.

0
source

You have an object, use ...

// Setup similar object
$searchMe = new stdClass;
$searchMe->Criteria = '';
$searchMe->Code = 275;
$searchMe->Type = 15;
$searchMe->Name = 'Pen';

var_dump(in_array('275', (array) $searchMe)); // bool(true)

CodePad .

When I passed ( (array)) to the array, I got ...

array(4) {
  ["Criteria"]=>
  string(0) ""
  ["Code"]=>
  int(275)
  ["Type"]=>
  int(15)
  ["Name"]=>
}
0
source

All Articles