Search multidimentional array and return key?

$userdb=Array
(
    (0) => Array
        (
            (uid) => '100',
            (name) => 'Sandra Shush',
            (url) => 'urlof100'
        ),

    (1) => Array
        (
            (uid) => '5465',
            (name) => 'Stefanie Mcmohn',
            (pic_square) => 'urlof100'
        ),

    (2) => Array
        (
            (uid) => '40489',
            (name) => 'Michael',
            (pic_square) => 'urlof40489'
        )
);

How do I search for uid and name and pic_square and then when matched unset that entire key?

For example i wanna check if this is in array:

 (uid) => '5465',
  (name) => 'Stefanie Mcmohn',
   (pic_square) => 'urlof100'

if so unset the its key I mean remove it from array?

So if this is results from a query wouldn’t it be better to do the searching for the uid, name etc… in the database with a query and get the results you are looking for?. Just some food for thought. But to accomplish your problem is well loops. So i threw it in a function as i was not sure on what area’s it was important to search. Most common would be the uid but i did not want to make an assumption. So this little function will loop through the array and see if it can match a uid or name or pic_square and then gets the parent arrays id and deletes it and returns true on success and false on failure.

function userSearch($user_id = null, $name = null, $pic_square = null)
{
	// check if it is an array and it has something in it
	if(is_array($userdb) && count($userdb) > 0)
	{
		foreach($userdb as $sub_array => $user)
		{
			if($user['uid'] == $user_id)
			{
				unset($userdb[$sub_array]);
				return true;
			}
			
			elseif(!is_null($name) && $user['name'] == $name)
			{
				unset($userdb[$sub_array]);
				return true;
			}
			
			elseif(!is_null($pic_square) && $user['pic_square'] == $pic_square)
			{
				unset($userdb[$sub_array]);
				return true;
			}
		}
	}
	
	return false;
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.