PHP - 找到对象的数组按对象属性条目 [英] PHP - find entry by object property from a array of objects

查看:207
本文介绍了PHP - 找到对象的数组按对象属性条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该数组如下:

[0] => stdClass Object
        (
            [ID] => 420
            [name] => Mary
         )

[1] => stdClass Object
        (
            [ID] => 10957
            [name] => Blah
         )
...

和我有叫整型变量 $ V

我怎么能选择具有当'ID'属性有 $ V 值对象的数组项?

How could I select a array entry that has a object where the 'ID' property has the $v value ?

推荐答案

您无论是遍历数组(在同一时间只能搜索OK)在搜索特定的记录,或使用其他关联数组建立一个HashMap中。

You either iterate the array, searching for the particular record (ok in a one time only search) or build a hashmap using another associative array.

对于前者,像这样

$item = null;
foreach($array as $struct) {
    if ($v == $struct->ID) {
        $item = $struct;
        break;
    }
}

看到这个问题,并随后回答了对后者的更多信息 - 参考PHP数组由多个索引

这篇关于PHP - 找到对象的数组按对象属性条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆