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 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化