tags:

views:

54

answers:

2

I have a result set from a DB that returns the following array.... how do I implode this into a comma delimited string? Thanks!

Array
(
    [0] => Array
        (
            [user_id] => 2
        )

    [1] => Array
        (
            [user_id] => 5
        )

    [2] => Array
        (
            [user_id] => 11
        )
)
+4  A: 
$t = array_map(function (array $a) { return $a["user_id"]; }, $original_array);
$result = implode(",", $t);

(PHP 5.3+, the closure must be turned into a regular function for earlier versions)

Artefacto
Don't think there is a need to confuse beginner PHP programmers with constructions of such complexity.
FractalizeR
@Fra Seriously? This is "complex"?
Artefacto
Yes. Closures is not something beginners can understand very well.
FractalizeR
@Fra I don't see why. `Scheme` is often taught in introductory programming courses. Do you think the beginner PHP programmer cannot understand something a freshman CS undergraduate can?
Artefacto
Do you see the level of the question asked? The guy doesn't know how to restructure a given array. Forget Scheme. ;)
FractalizeR
+1  A: 
$resultArray = array();
foreach($myNestedArray as $item) {
   $resultArray[]=$item['user_id'];
}
$resultString = implode(',', $resultArray);

Works on all recent PHP versions.

FractalizeR