tags:

views:

42

answers:

4

There is a variable $list, which has some array.

printr_r($list);

gives something like:

Array (
    [0] => stdClass Object (
        [ID] => 1
        [name] => Martha Dandridge
    )
    [1] => stdClass Object (
        [ID] => 35
        [name] => Abigail Smith
    )
    [2] => stdClass Object (
        [ID] => 153
        [name] => Julia Gardiner
    )
    [3] => stdClass Object (
        [ID] => 271
        [name] => Hillary Rodham
    )
    [4] => stdClass Object (
        [ID] => 124
        [name] => Nancy Davis
    )
)

We should check, if any entry of this array has wanted value in [name].

Like:

if($list has "Nancy Davis" or "Hillary Rodham" in [name] of some entry?) {
    return true;
} else {
    return false;
}

We have both "Nancy Davis" and "Hillary Rodham" in our arrey, so it should give "true".

If we ask like this:

  if($list has "George Bush" or "Lou Henry" or "Helen Herron" in [name] of some entry?) { ... }

It should give "false", because there aren't such values in any [name].

There can be any number of entries in array (I mean [0], [1] ... [any]), it should check [name] of each entry.

Hope you understand, if no - feel free to ask.

Thanks.

A: 
$match = false;
foreach($list as $value){
  if($value->name == "George Bush" || $value->name =="Lou Henry" || $value->name == "Helen Herron") {
    $match = true;
    break;
  }
}

if($match) {
  // do some if true
} else {
  // do dome if false
}
antyrat
Is it ok to use "or"? Doesn't work for me.
Happy
Can you provide what are you trying using my code?
antyrat
`or` is valid in Python but not in PHP. And even if you replace `or` with `||` it won't work. The `if` statement is just wrong.
Felix Kling
I've updated the code. @Felix Kling you're correct, 12 hours for the work speak for themselves.
antyrat
if($value->name == "George Bush" or "Lou Henry" or "Helen Herron") - this is true logically, but doesn't work.
Happy
A: 

http://www.php.net/manual/en/function.in-array.php simple as that

Christian Smorra
A: 

You should try use the php in_array function.

if (in_array('Martha Dandridge', $list) || in_array('Abigail Smith', $list)) {
    # do something based on the fact that the elements are in the array $list
} else {
    # do something else based on the fact the the elements do no exist in the array $list
}
Martin
don't understand
Happy
I just edited my code snippet, that should help you understand.
Martin
It should be a [name] value
Happy
A: 

You could also go with

if ($arr['Janice'] !== null || $arr['Bill'] !== null) {
...
}

henasraf