I want to return the first-level key of a multidimensional array based on the value of one of the sub-levels.
For example if I provide '@google.com' as 'Admin Emails' I want to return 'Google'.
My array is below.
$array = array (
'Google' => array (
'Allowed Emails' => array ('@google.com','@gmail.com' ),
'Admin Emails' => 'laz@google.com, kingkong@gmail.com', // This really is supposed to be a String, not an array
'Company Name' => 'Alphabet',
'Memberships' => array ('websites', 'google' ),
),
'Facebook' => array (
'Allowed Emails' => array ('@facebook.com','@facebook.co.uk' ),
'Admin Emails' => 'markmark@facebook.com, shezzer@facebook.com',
'Company Name' => 'Facebook Limited',
'Memberships' => array ('websites', 'facebook' ),
),
'Apple' => array (
'Allowed Emails' => array ('@apple.com','@icloud.com' ),
'Admin Emails' => 'timtheking@apple.com',
'Company Name' => 'Apple Inc',
'Memberships' => array ('computers', 'apple' ),
),
'Dell2' => array (
'Allowed Emails' => array ('@dell.com' ),
'Admin Emails' => 'mikerulez1996@dell.com',
'Company Name' => 'Dell Computers',
'Memberships' => array ('computers', 'dell' ),
)
);
I've managed to get a long way using array_filter but the below only works if the variable I've set (in this case 'google.com') is the first variable in the array.
Any thoughts much appreciated.
function setitem ($item){ return $item['Allowed Emails'][0] === '@google.com' ;}
$filteredarray = array_filter($array, setitem);
$toplevelarray = array_keys($filteredarray);
echo $toplevelarray[0];
Also I need some help passing the variable (in this case '@google.com' into the 'setitem' function as this won't be hard coded in. For some reason, I get an error if I try and do it the normal way.
Thanks in advance.