duanjian3920 2015-07-29 04:55
浏览 21
已采纳

有效地循环遍历数组

I have an array like this:

[1] => Array
        (
            [id] => 7
            [ticket_id] => 12
            [client_id] => 174
            [thread_name] => 
            [added_by] => 2
            [created_at] => 2015-07-28 23:24:07
            [updated_at] => 2015-07-28 23:24:07
            [notes] => Array
                (
                    [0] => Array
                        (
                            [id] => 21
                            [user_id] => 2
                            [notes] => Not all those who wander are lost!
                            [notes_attachment] => 
                            [deleted_at] => 
                            [created_at] => 2015-07-28 23:34:31
                            [updated_at] => 2015-07-28 23:34:31
                            [thread_id] => 7
                            [users] => Array
                                (
                                    [0] => Array
                                        (
                                            [user_id] => 1
                                        )

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

                                )

                        )

                )

        )

And I need to manipulate the users array in the notes array to something like this.

[users] => Array
(
[0] => 1
[1] => 2
)

So, basically, I need an array for ids in the users array.

I tried doing it, but it takes three loop to get into users.

How can I do that more efficiently ??

NOTE: This is just one of the structure from multiple arrays. Like, I have 4 threads and multiple notes in each of them.

Also, I need to manipulate the existing array and not just take the data from it.

My attempt:

$tmp = array();
        foreach($data as $value) {
            foreach($value['notes'] as $notes) {
                if(!empty($notes['users'])){
                    foreach($notes['users'] as $users) {
                        $tmp[] = $users['user_id'];
                        unset($notes['users']);
                        $notes['users'] = $tmp;
                    }
                }
            }
        }
  • 写回答

1条回答 默认 最新

  • dongwo5449 2015-07-29 05:00
    关注

    Use array_column -

    $new = array_column($a[notes][0]['users'], 'user_id');
    var_dump($new);
    

    Output

    array(2) {
      [0]=>
      int(1)
      [1]=>
      int(2)
    }
    

    DEMO

    Update

    $new = array();
    foreach($a as $value) {
        $new = array_merge($new, array_column($value[notes][0]['users'], 'user_id'));
    }
    var_dump($new);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。