简体   繁体   English

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

[英]How to get a value from within an object that is within an array

I'm trying to get data out of a variable called $items 我正在尝试从名为$ items的变量中获取数据

When I var_dump($items); 当我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 ...等等11次

I want to know if it is possible to get the email from $items with code that something like this: 我想知道是否可以从$ items获取带有以下代码的电子邮件:

$email = $items find the object where meta_key has the value "Email of Attendee" then return me the corresponding value. $email = $items找到meta_key值为"Email of Attendee"的对象,然后"Email of Attendee"我返回相应的值。

What I ended up doing was running $items through a foreach loop like so: 我最终要做的是通过一个foreach循环运行$items ,如下所示:

foreach($items as $item){

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

}

Which converts all the "meta_keys" into the values that they were referencing. 它将所有“ meta_keys”转换为它们所引用的值。 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. 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. 有更多经验的人可以建议一种直接从$ items获取“与会者电子邮件”的方法,而不必通过foreach循环来运行。

Still relying on the use of foreach loop. 仍然依赖于foreach循环的使用。

 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 更正您可以使用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 vs loop

array_filter() cannot handle [multi-dimensional arrays] natively. array_filter()无法本地处理[多维数组]。 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. array_filter()并不是执行此操作的最佳方法,因为当您找到想要的值时可以停止迭代array_filter()不会执行此操作。 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. 因为array_filter()是内置函数,所以它最有可能比手动编码的foreach -loop更快。 Stefan Gehrig Stefan Gehrig

Using a php foreach loop is probably the easier of the two to read: 使用php foreach循环可能更容易阅读两者:

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() : 但是,正如引言所假设的,对于更大的数组,您可能想要使用类似php的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 foreach可以遍历数组和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;
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM