I know the key, now I need ALL of the results that would yield when I search the 5000+ user database. Any user may have none, one or multiple locations, identified by an id
and name
field. Therefore I need the results in an array, not just the first/last one, but all of them. Below is an example of a user (actual array setup, fake data ;) )
Array
(
[ID] => 2
[user_login] => SomeGuy
[user_pass] => GreatEncryptedPassword
[user_nicename] => Some Guy
[user_email] => someguy@has-email.com
[user_url] => someguy.com
[user_registered] => 2013-04-11 11:18:58
[user_activation_key] =>
[user_status] => 0
[display_name] => Some Guy
[umeta_id] => 31
[user_id] => 2
[meta_key] => facebookmeta
[meta_value] => Array
(
[id] => 1234567890
[name] => Some Guy
[first_name] => Some
[last_name] => Guy
[link] => http://www.facebook.com/someguy
[username] => someguy
[birthday] => 02/21/1983
[location] => Array
(
[id] => 108276092536515 //actual ID, try at facebook.com/108276092536515
[name] => Arnhem, Netherlands
)
[gender] => male
[relationship_status] => In a Relationship
[significant_other] => Array
(
[name] => Some Chick
[id] => 12345678906789
)
[email] => someguy@has-email.com
[timezone] => 2
[locale] => nl_NL
[verified] => 1
[updated_time] => 2013-04-02T09:28:30+0000
)
)
As you can make out, this guy has 1 location in his facebook data, but other locations include the same [location] => Array ( [id] => 123 [name] => SomePlace )
for example for [work]
or [education]
, or, or, or...
I've tried a recursive function like this:
function get_value_by_key( $array, $key ){
foreach( $array as $k => $each ){
if( $k == $key ){
return $each;
}
if( is_array( $each )){
if( $return = get_value_by_key($each,$key)){
return $return;
}
}
}
}
print_r( get_value_by_key( $users, 'location' );
How can I modify the above function to return an array of location arrays? This one return the above data of the first user containing a location, not the location itself or an array of them.
EDIT: What I need as a result is something like this:
Array(
[0] => Array(
[id] => 1234567890
[name] => GreatCity, World
)
[1] => Array(
[id] => 2345678901
[name] => SomeCity, Blop
)
[2] => Array(
[id] => 3456789012
[name] => AnotherCity, Boo
)
)
Edit based on answer Answer from Thomas David Plat gives me the correct results, but still a not very usable data structure. How to get the results out of the structure and into that shown in the above edit?
Results from David's answer:
Array
(
[0] => Array
(
[0] => Array
(
[0] => Array
(
[id] => 108276092536515
[name] => Arnhem, Netherlands
)
[1] => Array()
)
)
[1] => Array
(
[0] => Array
(
[0] => Array
(
[id] => 108276092536515
[name] => Arnhem, Netherlands
)
[1] => Array()
)
)
[2] => Array()
[3] => Array()
[4] => Array()
I've tried variations of array_filter
, array_map
, array_values
and others like below:
$locations = array_map( 'array_filter', find_array_values( $users, 'location' ));
But the structure seems to stay...