Charles Yeung
Charles Yeung

Reputation: 38805

Search an array of objects for a particular column value

Suppose I have an array as below, how can I "check if vid = 2, then get/echo tid"?

Array
(
    [29] => stdClass Object
        (
            [tid] => 29
            [vid] => 2
            [name] => notebook
            [description] => 
            [weight] => 0
            [language] => 
            [trid] => 0
        )

    [97] => stdClass Object
        (
            [tid] => 97
            [vid] => 1
            [name] => DELL
            [description] => 
            [weight] => 0
            [language] => 
            [trid] => 0
        )

)

Upvotes: 1

Views: 78

Answers (2)

Charles Yeung
Charles Yeung

Reputation: 38805

it should be:

  foreach($tid as $key => $value){
    if($value->vid == 2){
        echo $value->tid;
    }
  } 

Upvotes: 0

Justin Lucas
Justin Lucas

Reputation: 2321

foreach($vids as $vid){
    if($vid->vid == 2){
        echo $vid->tid;
    }
}

Upvotes: 5

Related Questions