2

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.

user783322
  • 439
  • 8
  • 16

2 Answers2

1

in_array() looks in the values of the array. As I understand it, you want to search the keys, you wanna use array_key_exists():

        if(array_key_exists(1,$this->_props['test'])){
                echo "Correct result";
        }else{
                echo "Incorrect result";
        }

you should get the right result. If you're meaning to look for the values recursively, consider using array_find()

rdiz
  • 5,627
  • 1
  • 25
  • 36
  • hes searching in `this->_props` and not in `md_array`. Your answer would be right, if it would be the `md_array` – Fuzzyma Jan 15 '15 at 12:19
  • `$this->_props = ['test'=>$md_array]` so basically he is, just a level above. – rdiz Jan 15 '15 at 12:23
0

you have a multidim array, and so in_array will not work for you. you need an own function:

function in_array_r($needle, $haystack, $strict = false) {
    foreach ($haystack as $item) {
        if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
            return true;
        }
    }

    return false;
}

and in your code:

if(in_array_r(1, $this->_props['test'])){
                    echo "Correct result";
            }else{
                    echo "Incorrect result";
            }

look here: in_array() and multidimensional array

Community
  • 1
  • 1
goldlife
  • 1,764
  • 1
  • 23
  • 40
  • $this->props is NO multidimensional array – Fuzzyma Jan 15 '15 at 12:20
  • in my test it is: `print_r($this->_props['test'])` is : `Array ( [1] => Array ( [0] => 0 [1] => 1 ) [2] => Array ( [0] => 0 [1] => 1 ) [3] => Array ( [0] => 0 [1] => 1 ) )` – goldlife Jan 15 '15 at 12:21