I am unable to match a key in an array using in_array
, contrary to what I expect.
The array I am attempting to match is part of the _props
array, created using the magic method __set()
.
The following code returns the response Incorrect result
. Here is the code, I hope that it's fairly self-explanatory.
class foo{
private $_props;
public function __set($name, $val){
$this->_props[$name] = $val;
}
public function test(){
$md_array = array(
1 => array(0 => '0', 1 => '1'),
2 => array(0 => '0', 1 => '1'),
3 => array(0 => '0', 1 => '1')
);
$this->__set('test', $md_array);
if(in_array(1, $this->_props['test'])){
echo "Correct result";
}else{
echo "Incorrect result";
}
}
}
$a = new foo();
$a->test();
Can anyone explain this behaviour for me and offer an alternative?
if I var_dump $this->_props
I get the following response:
array
'test' =>
array
1 =>
array
0 => string '0' (length=1)
1 => string '1' (length=1)
2 =>
array
0 => string '0' (length=1)
1 => string '1' (length=1)
3 =>
array
0 => string '0' (length=1)
1 => string '1' (length=1)
Thanks in advance.