3

Simple one here, but hurting ones head.

echo in_array('275', $searchMe);

is returning false. But if I print the array out and then search it manually using my web browser I am able to see that the value exists in the array.

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

Extra information. The array has been coverted from an object to an array using

$searchMe = (array) $object;

Would it be because the values are not quoted? I have tried using the following with the in_array function:

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]
              ...
8
  • Dump that array after the conversion, did it retain the associative keys for the properties of the object, or did it convert the properties to numeric indexes? If it worked at all. Commented Mar 17, 2011 at 1:11
  • Note that error indicates that $searchMe is still an object. Commented Mar 17, 2011 at 1:13
  • ok. so in_array will not work on objects? Commented Mar 17, 2011 at 1:14
  • @claw: Seeing as how objects are not arrays, probably not, unless you can convert it properly. Commented Mar 17, 2011 at 1:16
  • @claw in_array(), funnily enough, works on arrays :). You can however cast objects to arrays to turn their property/values into an associative array. Commented Mar 17, 2011 at 1:16

3 Answers 3

1

One scenario I think that would be helpful.

If the array index that contains the value is 0, the returning value of in_array would be 0. If in our logic, 0 is considered as false, then there will be issues. Avoid this pattern in your code.

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

2 Comments

I agree with you, I have just encountered the case of index 0 and in_array return false. Might be use strict mode also help me in this case. But instead I use array_search comparable to! == false
This should be a programming anti-pattern, or languages should know how to handle this. :)
0

in_array can't see inside the ExtrasInfoType Object. Basically its comparing ExtrasInfoType Object to 275, which in this case returns false.

Comments

0

You have an object there, 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 cast ((array)) to an array, I got...

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

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.