dzm
dzm

Reputation: 23534

Multidimensional array into string in PHP

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

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

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

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

Upvotes: 2

Views: 97

Answers (2)

Vladislav Rastrusny
Vladislav Rastrusny

Reputation: 29975

$resultArray = array();
foreach($myNestedArray as $item) {
   $resultArray[]=$item['user_id'];
}
$resultString = implode(',', $resultArray);

Works on all recent PHP versions.

Upvotes: 1

Artefacto
Artefacto

Reputation: 97805

$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)

Upvotes: 4

Related Questions