0

Hey guys got an issue with the in_array not returning true.

my code is as follows:

if ( in_array( 'item_name', $this->conditions ) ) {
        print "test";
}

this is just a test code. the $this->conditions is set in someplace else in the files and it looks like this:

Array
(
[0] => Array
    (
        [operator] => 
        [property] => item_name
        [logic] => contains
        [value] => the age
    )

)

its not printing the "test"; what am i doing wrong ?

var_dump added below:

array (size=2)
0 => 
array (size=4)
  'operator' => string '' (length=0)
  'property' => string 'item_name' (length=9)
  'logic' => string 'contains' (length=8)
  'value' => string 'the age' (length=7)
1 => 
array (size=4)
  'operator' => string 'or' (length=2)
  'property' => string 'item_name' (length=9)
  'logic' => string 'ends' (length=4)
  'value' => string 'malouf' (length=6)
2
  • Do a var_dump of $this->conditions to see exactly what it contains (e.g. no spaces or other invisible characters) Commented Sep 28, 2013 at 11:47
  • $this->conditions is an object you can verify using is_object() function. $this->conditions[0] will do for you. Commented Sep 28, 2013 at 11:50

3 Answers 3

4

You have a nested array. Try this :

foreach ($this->conditions as $arr) {

    if ( in_array( 'item_name', $arr ) ) {
       print "test";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

the issue is the conditions can have multiple arrays
0

PHP DOC :

in_array — Checks if a value exists in an array

in_array() wont return true because there is no value "item_name". You have to extract the inner array first. This : in_array( 'item_name', $this->conditions[0]) will return true

Comments

0

please try this

foreach($this->conditions as  $condition){
 if(in_array( 'item_name',  $condition))
     echo 'test';
};

I hope this will help you

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.