如何从对象数组滤除对象? [英] How to filter out an object from an array of objects?

查看:151
本文介绍了如何从对象数组滤除对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如下得到对象的数组;我怎么能过滤掉(删除)有admins->成员= 11?

的对象

在这个例子中,所得到的数组对象将只具有第1对象[245]在开始

 阵列

    [245] => stdClass的对象
        (
            [名] => Programmation网站
            [说明] =>
            [公用] => 0
            [jointype] =>受控
            [grouptype] =>课程
            [membershiptype] =>会员
            [原因] =>
            [作用] =>导师
            [的ctime] => 2011-10-12 14点41分35秒
            [修改时间] => 0000-00-00 00:00:00
            [图片] =>
            [SESSION_ID] => 28
            [会话名称] => HIVER 2012
            [MEMBERCOUNT] => 1
            [收藏] =>
            [要求] => 0
            [ID] => 245
            [委员] =>排列
                (
                    [0] => stdClass的对象
                        (
                            [ID] => 11
                            [名] =>伊夫·奥蒂斯(otisyves)
                        )                )            [项目] =>排列
                (
                    [0] => stdClass的对象
                        (
                            [ID] => 1923
                            [标题] => SANS滴度(1)
                            [说明] =>
                            [车主] => 11
                            [ownerformat] =>
                            [组] =>
                            [机构] =>
                            [STARTDATE] =>
                            [stopdate] =>
                            [的ctime] => 2011-10-12 13点24分26秒
                            [修改时间] => 2011-10-12 13点24分26秒
                            [的atime] => 2011-10-12 13点24分26秒
                            [submittedgroup] => 245
                            [submittedhost] =>
                            [submittedtime] => 2011-10-12 17点00分41秒
                            [为numColumns] => 3
                            [布局] =>
                            [主题] =>
                            [模板] => 0
                            [copynewuser] => 0
                            [类型] = GT;投资组合
                            [探访] => 0
                            [的allowComments] => 1
                            [approvecomments] => 0
                            [技巧] =>
                            [说明] =>
                            [accessconf] =>
                            [图片] =>
                            [competencesvisees] =>
                            [consignes] =>
                            [fichierconsignes] =>
                            [groupevise] =>
                        )                )            [project_count] => 1
            [管理员] =>排列
                (
                    [0] => stdClass的对象
                        (
                            [员] => 11
                            [名字] =>伊夫
                            [姓氏] =>奥的斯
                        )                )            [TOPIC_NAME] =>乐PHP
            [activites] =>排列
                (
                    [0] => stdClass的对象
                        (
                            [TOPIC_NAME] =>
                            [topic_id] => 42
                            [post_parent] => 107
                            [post_body] => OUI MOI进行相反的乙脑我demande CA。
                            [POST_ID] => 109
                        )                )            [论坛] =>排列
                (
                    [0] => stdClass的对象
                        (
                            [FORUM_NAME] =>讨论兴业银行
                            [forum_id] => 101
                        )                )        )    [246] => stdClass的对象
        (
            [名] =>莱斯立足德最近搜索
            [说明] =>
            [公用] => 0
            [jointype] =>受控
            [grouptype] =>课程
            [membershiptype] =>管理员
            [原因] =>
            [作用] =>管理员
            [的ctime] => 2011-10-13 15时27分30秒
            [修改时间] => 0000-00-00 00:00:00
            [图片] =>
            [SESSION_ID] => 27
            [会话名称] => 2011秋季艺术
            [MEMBERCOUNT] => 0
            [收藏] =>
            [要求] => 0
            [ID] => 246
            [project_count] => 0
            [TOPIC_NAME] => difficulte的
            [activites] =>排列
                (
                    [0] => stdClass的对象
                        (
                            [TOPIC_NAME] =>
                            [topic_id] => 44
                            [post_parent] => 111
                            [post_body] => Ouah!
                            [POST_ID] => 112
                        )                )            [论坛] =>排列
                (
                    [0] => stdClass的对象
                        (
                            [FORUM_NAME] => MySQL的乐
                            [forum_id] => 103
                        )                )        ))

我要检查对指数


解决方案

  $ ARR = array_filter($ ARR,功能($项目)
{
    如果(使用isset($本期特价货品>!管理员)|| is_array($本期特价货品>!管理员))
    {
        返回false;
    }
    的foreach($本期特价货品>管理员为$管理员)
    {
        如果($管理 - >成员== 11)
        {
            返回true;
        }
    }
    返回false;
});

(PHP 5.3中使用匿名函数)

I've got an array of objects as follows ; how can I filter out (remove) the objects that have admins->member = 11 ?

In this example, the resulting array object would only have the 1st object that [245] at the start.

Array
(
    [245] => stdClass Object
        (
            [name] => Programmation Web
            [description] => 
            [public] => 0
            [jointype] => controlled
            [grouptype] => course
            [membershiptype] => member
            [reason] => 
            [role] => tutor
            [ctime] => 2011-10-12 14:41:35
            [mtime] => 0000-00-00 00:00:00
            [image] => 
            [session_id] => 28
            [session_name] => Hiver 2012
            [membercount] => 1
            [favorite] => 
            [requests] => 0
            [id] => 245
            [members] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 11
                            [name] => Yves Otis (otisyves)
                        )

                )

            [projects] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 1923
                            [title] => Sans titre (1)
                            [description] => 
                            [owner] => 11
                            [ownerformat] => 
                            [group] => 
                            [institution] => 
                            [startdate] => 
                            [stopdate] => 
                            [ctime] => 2011-10-12 13:24:26
                            [mtime] => 2011-10-12 13:24:26
                            [atime] => 2011-10-12 13:24:26
                            [submittedgroup] => 245
                            [submittedhost] => 
                            [submittedtime] => 2011-10-12 17:00:41
                            [numcolumns] => 3
                            [layout] => 
                            [theme] => 
                            [template] => 0
                            [copynewuser] => 0
                            [type] => portfolio
                            [visits] => 0
                            [allowcomments] => 1
                            [approvecomments] => 0
                            [skills] => 
                            [instructions] => 
                            [accessconf] => 
                            [image] => 
                            [competencesvisees] => 
                            [consignes] => 
                            [fichierconsignes] => 
                            [groupevise] => 
                        )

                )

            [project_count] => 1
            [admins] => Array
                (
                    [0] => stdClass Object
                        (
                            [member] => 11
                            [firstname] => Yves
                            [lastname] => Otis
                        )

                )

            [topic_name] => Le PHP
            [activites] => Array
                (
                    [0] => stdClass Object
                        (
                            [topic_name] => 
                            [topic_id] => 42
                            [post_parent] => 107
                            [post_body] => Oui moi aussi je me demande ça.
                            [post_id] => 109
                        )

                )

            [forums] => Array
                (
                    [0] => stdClass Object
                        (
                            [forum_name] => Discussion générale
                            [forum_id] => 101
                        )

                )

        )

    [246] => stdClass Object
        (
            [name] => Les bases de données
            [description] => 
            [public] => 0
            [jointype] => controlled
            [grouptype] => course
            [membershiptype] => admin
            [reason] => 
            [role] => admin
            [ctime] => 2011-10-13 15:27:30
            [mtime] => 0000-00-00 00:00:00
            [image] => 
            [session_id] => 27
            [session_name] => Automne 2011
            [membercount] => 0
            [favorite] => 
            [requests] => 0
            [id] => 246
            [project_count] => 0
            [topic_name] => Difficulté
            [activites] => Array
                (
                    [0] => stdClass Object
                        (
                            [topic_name] => 
                            [topic_id] => 44
                            [post_parent] => 111
                            [post_body] => Ouah!
                            [post_id] => 112
                        )

                )

            [forums] => Array
                (
                    [0] => stdClass Object
                        (
                            [forum_name] => Le MySQL
                            [forum_id] => 103
                        )

                )

        )

)

I'd like to check against the index

解决方案

$arr = array_filter($arr, function($item)
{
    if(!isset($item->admins) || !is_array($item->admins))
    {
        return false;
    }
    foreach($item->admins as $admin)
    {
        if($admin->member == 11)
        {
            return true;
        }
    }
    return false;
});

(Using anonymous function in PHP 5.3)

这篇关于如何从对象数组滤除对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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