从多维数组中提取值并放置在逗号分隔的字符串中 [英] Extract value from multidimensional array and place in comma separated string

查看:150
本文介绍了从多维数组中提取值并放置在逗号分隔的字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 数组

[1] => ;数组

[name] => Zeze
[city] => Denver,
[state] => Colorado,
[country] = > United States
[user_id] => 1
[cars] => Array

[140] => Array

[cars_name] => BMW


[162] =>阵列

[cars_name] =>马自达



$ b [8] =>数组

[name] => Lex
[city] => Schwelm,
[state] => North Rhine-Westphalia,
[country] => Germany
[user_id] => 5
[cars] =>阵列

[140] =>阵列

[cars_name] =>奔驰


[162] = >阵列

[cars_name] =>奥迪




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

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

1,5 code

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

解决方案

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

假设你的数组被命名为 $ users $ list 是输出。


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.

这篇关于从多维数组中提取值并放置在逗号分隔的字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆