0

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

0

1 Answer 1

3
$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;
});
Sign up to request clarification or add additional context in comments.

8 Comments

might want to also use if (property_exists($item, 'admins')) to make sure the object actually has an admins var. The question isnt clear whether his objects are homogenous or not
Hi carpii, yes indeed we should check if the objects are homogenous first ... Tim, how can I filter out the abovementionned array using a for loop ? Because it seems like the answer explicitly using the index 0 because in my example this is where arises the $item->admins[0]->member == 11, but what if I don't know which object has ? Thanks !
@Tim: Thanks! But now my array is empty after it passed through this callback function ...
@Alex: If you assign the value of the expression back to the original array, you will overwrite it. Also, if there are no admins with the member == 11, then the array should be empty.
@Tim: What do you mean "if you assigne the expression back to the original array" ? I'Ve used the same array as in the example above ...
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.