从多维数组中提取值并放在逗号分隔的字符串中

I have an array that looks like this

Array
(
    [1] => Array
        (
            [name] => Zeze
            [city] => Denver, 
            [state] => Colorado, 
            [country] => United States
            [user_id] => 1
            [cars] => Array
                (
                    [140] => Array
                        (
                            [cars_name] => BMW
                        )

                    [162] => Array
                        (
                            [cars_name] => Mazda
                        )
                )
        )

    [8] => Array
        (
            [name] => Lex
            [city] => Schwelm, 
            [state] => North Rhine-Westphalia, 
            [country] => Germany
            [user_id] => 5
            [cars] => Array
                (
                    [140] => Array
                        (
                            [cars_name] => Mercedes
                        )

                    [162] => Array
                        (
                            [cars_name] => Audi
                        )
                )
        )
)

I need to extract the value from user_id and put it in a comma separated string.

For the above array, I would like to get:

1,5

I'm a bit confused how to loop this array with foreach and then how would I create the string? Or is there a better way?

$uids = Array();
foreach($users as $u) $uids[] = $u['user_id'];
$list = implode(",",$uids);

This is assuming your array is named $users and $list is the output.

Iterate over each item in the multimensional array with a foreach loop, and treat the item as a normal array. Then push the user_id value into another array and implode it with a comma to make it comma separated.

$user_ids = array();

foreach($arr in $multidim_arr) {
    array_push($user_ids, $arr["user_id"]);
}

$user_ids = implode(",", $user_ids);

You can use a combination of array_map and implode:

function get_uid($el) {
    return $el["user_id"];
}

$csv = implode(array_map("get_uid", $your_array), ',');
echo $csv;    

This will be the most easier method:

echo implode(",", array_column($myArray, "user_id"));

$stateId = Array (
     [0] => Array
         (
             [id] => 9
             [state_id] => 81
             [rto_id] => 82
             [is_active] => 1
         )
     [1] => Array
         (
             [id] => 10
             [state_id] => 82
             [rto_id] => 83
             [is_active] => 1
         )

 );

 $stateIds = implode(",", array_column($stateId, "state_id"));

 echo $stateIds;