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