php - How to add subvalues to a new array? -



php - How to add subvalues to a new array? -

i have array every containing value beingness array 1 value. looking way flatten array , succeeded somehow having feeling can done better. best way or can still improved?

<?php $array = array( '1' => array('id' => '123'), '2' => array('id' => '22'), '3' => array('id' => '133'), '4' => array('id' => '143'), '5' => array('id' => '153'), ); array_walk_recursive($array, function($v, $k) utilize (&$result) { $result[] = $v; });

you can accomplish using array_map function:

$func = function($value) { homecoming $value['id']; }; $array2 = array_map($func, $array);

or if want maintain in 1 line do:

$array2 = array_map(function($value) { homecoming $value['id']; }, $array);

this homecoming array flattened , keeps initial keys:

array(5) { [1]=> string(3) "123" [2]=> string(2) "22" [3]=> string(3) "133" [4]=> string(3) "143" [5]=> string(3) "153" }

if don't want maintain keys, phone call next @ end:

$array2 = array_values($array2);

php arrays multidimensional-array

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -