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.

5
  • you are testing the value this->_props['test'] which is whatevery you assined to foo->test but it is NOT an array at the first place Commented Jan 15, 2015 at 12:18
  • Thanks, but this works var_dump($this->_props['test'][1]);. It gets the correct key and val?? Commented Jan 15, 2015 at 12:21
  • Well - the array $this->_props is dynamical. We dont know what is in there. Can you pls add which $value is added under the $key test? Commented Jan 15, 2015 at 12:22
  • I've added to the original Commented Jan 15, 2015 at 12:25
  • btw why you are using magic function to set some array values ? Commented Jan 15, 2015 at 12:29

2 Answers 2

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()

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

2 Comments

hes searching in this->_props and not in md_array. Your answer would be right, if it would be the md_array
$this->_props = ['test'=>$md_array] so basically he is, just a level above.
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

2 Comments

$this->props is NO multidimensional array
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 ) )

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.