Implode a multidimensional array?

my goal is to create a string like this:
1603,1802,2255

but i start out with this multidimensional array:
Array
(
[0] => Array
(
[device_id] => 1603
)

[1] => Array
    (
        [device_id] => 1802
    )

[2] => Array
    (
        [device_id] => 2255
    )

)

what’s the best way to change the multidimensional array to this:
array(1603,1802,2255);

so i can implode it?

You could create a temporary array with the required values, then implode the contents.


$deviceIds = array();
foreach ($multiDimArray as $item) {
    $deviceIds[] = $item['device_id'];
}
$str = implode(',', $deviceIds);

From there you could then attempt to simplify things by using something like array_map to create the modified array.

Alternative ways of doing the same:


function device_ids(&$value) {
	$value = $value['device_id'];
}

array_walk($multiDimArray, 'device_ids');
// changes are applied to $multiDimArray


function device_ids($value) {
	return $value['device_id'];
}

$singleDimArray = array_map('device_ids', $multiDimArray);
// $multiDimArray is left unchanged
// the result is written to $singleDimArray