如何从数组中的对象中获取值

I'm trying to get data out of a variable called $items

When I var_dump($items); - the result is like this:

array(13) { 

[0]=> object(stdClass)#868 (2) { 
        ["meta_key"]=> string(17) "Email of Attendee"        
        ["meta_value"]=> string(68) "some-email@gmail.com" 
} 

[2]=> object(stdClass)#804 (2) { 
        ["meta_key"]=> string(28) "Name to be printed on badge:" 
        ["meta_value"]=> string(7) "some name to be printed" 
}

...and so on 11 more times

I want to know if it is possible to get the email from $items with code that something like this:

$email = $items find the object where meta_key has the value "Email of Attendee" then return me the corresponding value.

What I ended up doing was running $items through a foreach loop like so:

foreach($items as $item){

    $items[$item->meta_key]=$item->meta_value;

}

Which converts all the "meta_keys" into the values that they were referencing. now:

$email = $items["Email of Attendee"]  

echo $email; 

result is some-email@gmail.com

Posting this so that a. someone else in a similar jam might use the for each loop that converts things

b. someone with more experience can suggest a way to get the "Email of Attendee directly from the $items, without having to run it through a foreach loop.

Still relying on the use of foreach loop.

function get_email($items) {

    foreach($items as $item){

        if (in_array("Email of Attendee", $item) {
            $email = $item["meta_value"];
            break;
        }

    }
    return $email;
}

Correction You can get the particular object with array_filter

$result = array_filter($array, function($o) {
                   return $o->meta_key == "Email of Attendee";
});

$email = $result[0]->meta_value;

echo $email;

This should do the magic.

foreach($items as $item){

    // $item is already holding the object here. Equals to $items[0] in the first loop
    if($item->meta_key == "Email of Attendee"){
        // do stuff
    }

}

Quoted from Search Array : array_filter vs loop:

array_filter() cannot handle [multi-dimensional arrays] natively. You're looking for a single value inside an array? array_filter() is not the best way to do this because you can stop iteration when you found the value you've been looking for - array_filter() doesn't do that. Filter a set of values from a larger set? Most likely that array_filter() is faster than a hand-coded foreach-loop because it's a built-in function. – Stefan Gehrig

Using a php foreach loop is probably the easier of the two to read:

function getItem($haystack, $needle) {
  foreach ($haystack as $hay) {
    if ($hay->meta_key == $needle) {
      return $hay->meta_value;
    }
  }
  return FALSE;
}

echo getItem($items, 'Email of Attendee'); // Returns 'some-email@gmail.com'

However, as the quote supposes, for a larger array, you may want to go with something like php's array_filter():

function metaKeyIsEmail($obj) {
  return $obj->meta_key == 'Email of Attendee';
}

// array_filter() will return an array containing all items
// that returned TRUE for the callback metaKeyIsEmail()
$items_matched = array_filter($items, 'metaKeyIsEmail');

// If there was at least one match, take it off the front of
// the array and get its meta_value. Otherwise use FALSE.
$matched_value = !empty($items_matched) ? array_shift($items_matched)->meta_value : FALSE;

echo $matched_value; // Returns 'some-email@gmail.com'

foreach can iterate through array as well as object

$given_array = array((object)array('meta_key'=>'email','mea_value'=>'fg'),
                     (object)array('meta_key'=>'email','mea_value'=>'gfdgf'));


foreach($given_array as $elt){
    foreach($elt as $key=>$value){
                if($key == "Email of Attendee"){
                    echo $email;
        }
}