doujiu4643
2011-09-20 03:07
浏览 169
已采纳

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

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?

图片转代码服务由CSDN问答提供 功能建议

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

  Array \  n(
 [1] =>数组
(
 [名称] => Zeze 
 [城市] =>丹佛,
 [州] =>科罗拉多州,
 [国家] =&gt  ;美国
 [user_id] => 1 
 [cars] =>数组
(
 [140] =>数组
(
 [cars_name] =>宝马
)
  
 [162] =>数组
(
 [cars_name] =>马自达
)
)
)
 
 [8] =>数组
(
 [名称] =  > Lex 
 [city] => Schwelm,
 [state] => North Rhine-Westphalia,
 [country] => Germany 
 [user_id] => 5 
 [cars] =  >数组
(
 [140] => 数组
(
 [cars_name] =>梅赛德斯
)
 
 [162] => 数组
(
 [cars_name] =>奥迪
)
)
)
)
   
 
 

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

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

< 代码> 1,5

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • dougui1977 2011-09-20 03:11
    已采纳
    $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.

    点赞 评论
  • dongzhong2674 2011-09-20 03:10

    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);
    
    点赞 评论
  • doo6568 2011-09-20 03:12

    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;    
    
    点赞 评论
  • dragon_9000 2017-06-05 14:07

    This will be the most easier method:

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

    点赞 评论
  • ds0409 2018-01-04 14:57
    $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;
    
    点赞 评论

相关推荐 更多相似问题