如何从对象数组中过滤掉对象?

时间:2011-10-18 23:14:23

标签: php arrays

我有一个对象数组如下;如何过滤掉(删除)具有admins-> member = 11?

的对象

在这个例子中,生成的数组对象只有第一个对象[245]在开始。

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
                        )

                )

        )

)

我想检查索引

1 个答案:

答案 0 :(得分:2)

$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;
});

(在PHP 5.3中使用匿名函数)