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:开发者_Go百科 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]
...
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.
in_array
can't see inside the ExtrasInfoType Object
. Basically its comparing ExtrasInfoType Object
to 275
, which in this case returns false
.
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"]=>
}
精彩评论