NotionCommotion Posted October 24, 2016 Share Posted October 24, 2016 Recommendations on the best way to extract only specific elements from an array? What about the following? Thanks while($source=$stmt->fetch(\PDO::FETCH_ASSOC)) { if($source['guid']) { $clients[]=array_intersect_key($source, ['name'=>null,'guid'=>null]); } else { $servers[]=array_intersect_key($source, ['name'=>null,'ip'=>null,'port'=>null,'encrypt_key'=>null]); } } Quote Link to comment Share on other sites More sharing options...
requinix Posted October 24, 2016 Share Posted October 24, 2016 I'd either do that or the obvious solution, mostly depending on whether I remembered about array_intersect_key at the time. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 24, 2016 Share Posted October 24, 2016 (edited) or simply $clients[] = [ 'name' => $source['name'], 'guid' = $source['guid'] ]; [EDIT] Requinix's turn to beat me to the post Edited October 24, 2016 by Barand 1 Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted October 24, 2016 Author Share Posted October 24, 2016 I'd either do that or the obvious solution, mostly depending on whether I remembered about array_intersect_key at the time. Obvious being $clients[]=['name'=>$source['name'],'guid'=>$source['guid']]; with maybe some isset checks? Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted October 24, 2016 Author Share Posted October 24, 2016 or simply $clients[] = [ 'name' => $source['name'], 'guid' = $source['guid'] ]; You beat me by 1 second! Would you recommend doing it your way if there are only a couple of elements, but using array_intersect_key() if there are more than say 5? Quote Link to comment Share on other sites More sharing options...
Barand Posted October 24, 2016 Share Posted October 24, 2016 (edited) Possibly, it is a neat solution for those who understand the function. Another option may be to organize your query so you can use array_slice(). Edited October 24, 2016 by Barand Quote Link to comment Share on other sites More sharing options...
requinix Posted October 24, 2016 Share Posted October 24, 2016 (edited) but using array_intersect_key() if there are more than say 5?You aren't really saving yourself much: still typing out the basic key => value stuff for each element you want. A sort of "array_choose" function would be nice to have built-into PHP. I wouldn't implement it in userland just for this one purpose, though. /** * Extract a subset of an array * * @param array $array Source array * @param array|scalar $key An array of keys, or the first key * @param scalar ... Additional keys if $key is not an array * @return array */ function array_choose(array $array, ...$keys) { $return = array(); foreach (isset($keys[0]) && is_array($keys[0]) ? $keys[0] : $keys as $key) { if (array_key_exists($array, $key)) { $return[$key] = $array[$key]; } } return $return; } Edited October 24, 2016 by requinix switching out isset for array_key_exists Quote Link to comment Share on other sites More sharing options...
kicken Posted October 24, 2016 Share Posted October 24, 2016 You could use array_fill_keys to build the second parameter to array_intersect_key easier for larger arrays. if($source['guid']) { $clients[]=array_intersect_key($source, array_fill_keys(['name','guid'], null)); } else { $servers[]=array_intersect_key($source, array_fill_keys(['name','ip','port','encrypt_key'], null)); } 1 Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted October 25, 2016 Author Share Posted October 25, 2016 You could use array_fill_keys to build the second parameter to array_intersect_key easier for larger arrays. Or array_flip() Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.