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

发布于 2024-12-05 10:35:33 字数 1365 浏览 1 评论 0原文

我有一个看起来像这样的数组,

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

我需要从 user_id 中提取值并将其放入逗号分隔的字符串中。

对于上面的数组,我想得到:

1,5

我有点困惑如何使用 foreach 循环这个数组,然后如何创建字符串?或者有更好的方法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

滴情不沾 2024-12-12 10:35:33
$uids = Array();
foreach($users as $u) $uids[] = $u['user_id'];
$list = implode(",",$uids);

假设您的数组名为 $users 并且 $list 是输出。

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

北方。的韩爷 2024-12-12 10:35:33

您可以结合使用 array_map内爆

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

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

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;    
删除会话 2024-12-12 10:35:33

使用 foreach 循环迭代多维数组中的每个项目,并将该项目视为普通数组。然后将 user_id 值推入另一个数组,并用逗号将其内爆,使其以逗号分隔。

$user_ids = array();

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

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

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);
放血 2024-12-12 10:35:33

这将是最简单的方法:

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

This will be the most easier method:

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

一向肩并 2024-12-12 10:35:33
$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;
$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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文