[Edit] From multidimensional array to single dimension
Example$array = array( array( 'id' => '1', 'name' => 'Scott', 'email' => '[email protected]' ), array( 'id' => '4', 'name' => 'John', 'email' => '[email protected]' ), array( 'id' => '56', 'name' => 'Chris', 'email' => '[email protected]' ) ); print_r(array_pivot($array, 'id', 'name')); /* Array ( [1] => Scott [4] => John [56] => Chris ) */ print_r(array_pivot($array, null, 'email')); /* Array ( [0] => [email protected] [1] => [email protected] [2] => [email protected] ) */ function array_pivot($array, $key, $value) { if(!is_array($array)) return false; if(count($array) === 0) return array(); $ret = array(); foreach($array as $ray) { if(is_null($key)) { if(array_key_exists($value, $ray)) { $ret[] = $ray[$value]; } } elseif(array_key_exists($key, $ray) && array_key_exists($value, $ray)) { $ret[$ray[$key]] = $ray[$value]; } } return $ret; } |
This Page is Under Construction! - If You Want To Help Please Send your CV - Advanced Web Core (BETA)
© Advanced Web Core. All rights reserved