Multidimensional Arrays in PHP -



Multidimensional Arrays in PHP -

i'm having problem multidimensional arrays in php.

i'm assuming should simple but, i'm having difficulty wrapping mind around multi-dimensions.

the array have looks this:

array ( [0] => array ( [projects_users] => array ( [project_id] => 1 ) ) [1] => array ( [projects_users] => array ( [project_id] => 2 ) ) )

i somehow alter array this, see array of project_id:

array ( [0] => 1 [1] => 2 )

sorry such simple question. hints or clues great!

$arr = ... $new_arr = array(); foreach ( $arr $el ) { $new_arr[] = $el['projects_users']['project_id']; }

or, php version >= 5.3:

$new_arr = array_map(function ($e) { homecoming $e['projects_users']['project_id']; }, $arr);

a 3rd fun way, reset:

$arr = ... $new_arr = array(); foreach ( $arr $el ) { $new_arr[] = reset(reset($el)); } performance

out of curiosity / boredom benchmarked iterative / functional styles , without reset. surprised see test 4 winner in every run — thought array_map had bit more overhead foreach, (at to the lowest degree in case) these tests show otherwise! test code here.

$ php -v php 5.3.4 (cli) (built: dec 15 2010 12:15:07) copyright (c) 1997-2010 php grouping zend engine v2.3.0, copyright (c) 1998-2010 zend technologies $ php test.php test 1, iterative - 34.093856811523 microseconds test 2, array_map - 37.908554077148 microseconds test 3, iterative reset - 107.0499420166 microseconds test 4, array_map reset - 25.033950805664 microseconds $ php test.php test 1, iterative - 32.186508178711 microseconds test 2, array_map - 39.100646972656 microseconds test 3, iterative reset - 35.04753112793 microseconds test 4, array_map reset - 24.080276489258 microseconds $ php test.php test 1, iterative - 31.948089599609 microseconds test 2, array_map - 36.954879760742 microseconds test 3, iterative reset - 32.901763916016 microseconds test 4, array_map reset - 24.795532226562 microseconds $ php test.php test 1, iterative - 29.087066650391 microseconds test 2, array_map - 34.093856811523 microseconds test 3, iterative reset - 33.140182495117 microseconds test 4, array_map reset - 25.98762512207 microseconds

php arrays multidimensional-array

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -