knot22
knot22

Reputation: 2768

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
)
*/

Upvotes: 1

Views: 117

Answers (2)

Salketer
Salketer

Reputation: 15711

You're a bit lazy aren't you?

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

Upvotes: 1

bwoebi
bwoebi

Reputation: 23787

This little snippet returns your wished array:

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

Upvotes: 0

Related Questions