misulicus
misulicus

Reputation: 415

problems with php in_array

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)

Upvotes: 0

Views: 660

Answers (3)

Mohammad Ismail Khan
Mohammad Ismail Khan

Reputation: 651

please try this

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

I hope this will help you

Upvotes: 0

S.Thiongane
S.Thiongane

Reputation: 6905

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

Upvotes: 0

Fouad Fodail
Fouad Fodail

Reputation: 2643

You have a nested array. Try this :

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

    if ( in_array( 'item_name', $arr ) ) {
       print "test";
    }
}

Upvotes: 4

Related Questions