简体   繁体   中英

how to consolidate results of an array

I'm wondering how to go about consolidating an array. The $all array, shown below, is an example of data in an array that is actually being returned from a database query. The goal is to put the data into a single dimension array using integers as the keys in the final array.

$all = array(0=>array('ItemID' => 12), 1=>array('ItemID' => 13), 2=>array('ItemID' => 14));
echo "<pre>" . print_r($all, 1) . "</pre>";

/*
goal for format of consolidated array:
Array
(
    [0] => 12
    [1] => 13
    [2] => 14
)
*/

You're a bit lazy aren't you?

$data = array();
foreach($all as $k=>$v)
    $data[] = $v['ItemID']:

这个小片段返回您想要的数组:

$all = array_map(function($val) { return current($val); }, $all);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM