php - Get values (plural) from multi-dimensional array with key -


i know key, need all of results yield when search 5000+ user database. user may have none, 1 or multiple locations, identified id , name field. therefore need results in array, not first/last one, of them. below example of user (actual array setup, fake data ;) )

array ( [id] => 2 [user_login] => someguy [user_pass] => greatencryptedpassword [user_nicename] => 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] => guy [umeta_id] => 31 [user_id] => 2 [meta_key] => facebookmeta [meta_value] => array     (         [id] => 1234567890         [name] => guy         [first_name] =>         [last_name] => guy         [link] => http://www.facebook.com/someguy         [username] => someguy         [birthday] => 02/21/1983         [location] => array             (                 [id] => 108276092536515   //actual id, try @ facebook.com/108276092536515                 [name] => arnhem, netherlands             )          [gender] => male         [relationship_status] => in relationship         [significant_other] => array             (                 [name] => chick                 [id] => 12345678906789             )          [email] => someguy@has-email.com         [timezone] => 2         [locale] => nl_nl         [verified] => 1         [updated_time] => 2013-04-02t09:28:30+0000     ) ) 

as can make out, guy has 1 location in facebook data, other locations include same [location] => array ( [id] => 123 [name] => someplace ) example [work] or [education], or, or, or...

i've tried recursive function this:

function get_value_by_key( $array, $key ){     foreach( $array $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 modify above function return array of location arrays? 1 return above data of first user containing location, not location or array of them.

edit: need result 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 thomas david plat gives me correct results, still not usable data structure. how results out of structure , shown in above edit?

results 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 , others below:

 $locations = array_map( 'array_filter', find_array_values( $users, 'location' )); 

but structure seems stay...

updated answer

$matches = array();  function find_array_values($haystack, $needle) {      global $matches;     $collection = array();      foreach($haystack $key => $value)     {         if($key === $needle)         {             $matches[] = $value;         }         else if(is_array($value))         {            find_array_values($value, $needle);         }     } }  find_array_values($array, 'location'); print_r($matches); ?> 

this work, said in comments it's bad practice since uses global keyword. if implement function method class, instead of using global keyword use class variable neat solution.

old answer

function find_array_values($haystack, $needle) {     $collection = array();      foreach($haystack $key => $value)     {         if($key === $needle)         {             $collection[] = $value;         }         else if(is_array($value))         {            $collection[] = find_array_values($value, $needle);         }     }      return $collection;  }  echo "<pre>"; print_r(find_array_values($users, 'location')); echo "</pre>"; 

here are. pass array of users function. function return array of arrays of locations.

there's 1 restriction. function not search trough locations within locations.


Comments

Popular posts from this blog

php - mySql Join with 4 tables -

css - Text drops down with smaller window -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -