亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

php - mysql one-to-many result classification
ringa_lee
ringa_lee 2017-06-29 10:08:19
0
1
832

I found a lot of data from the database, one by one, a two-dimensional array, the format is as follows:

Array
        (
            [0] => Array
                (
                    [id] => 29
                    [user_id] => 1
                    [car_no] => 234567
                )

            [1] => Array
                (
                    [id] => 21
                    [user_id] => 1
                    [car_no] => 23565
                )

            [2] => Array
                (
                    [id] => 23
                    [user_id] => 1
                    [car_no] => 12345
                )

            [3] => Array
                (
                    [id] => 28
                    [user_id] => 1
                    [car_no] => 124455
                )

            [4] => Array
                (
                    [id] => 6
                    [user_id] => 1
                    [car_no] => 12345
                )

            [5] => Array
                (
                    [id] => 7
                    [user_id] => 2
                    [car_no] => 22
                )

            [6] => Array
                (
                    [id] => 3
                    [user_id] => 2
                    [car_no] => 粵T863
                )

            [7] => Array
                (
                    [id] => 25
                    [user_id] => 3
                    [car_no] => 12345
                )
                );

What should I do to classify users with the same user_id? The format is as follows:

Array
(
    [0] => Array
        (
            [user_id] => 8
            [login] => 18620982882
            [car_no] => Array
                (
                    [0] => 粵A123
                )

        )

    [1] => Array
        (
            [user_id] => 7
            [login] => 
            [car_no] => Array
                (
                    [0] => 粵T456
                    [1] => 123456
                )

        )

    [2] => Array
        (
            [user_id] => 2
            [login] => 13318029123
            [car_no] => Array
                (
                    [0] => 粵T863
                    [1] => 22
                )

        )

    [3] => Array
        (
            [user_id] => 1
            [login] => 15088138542
            [car_no] => Array
                (
                    [0] => 12345
                    [1] => 23565
                    [2] => 12345
                    [3] => 124455
                    [4] => 234567
                )

        )
        )

My approach is as follows, which achieves the goal, but it feels too inefficient:

    //先把id去重組成一個(gè)數(shù)組,計(jì)算需要處理的$data的子數(shù)組個(gè)數(shù),然后遍歷,如果user_id與去重后的那個(gè)數(shù)組的鍵值相同,就把這個(gè)值存到新數(shù)組里面去,組成一個(gè)新的二維數(shù)組。
    private function group_sort($unique, $num, $data) {
        foreach ($unique as $key => $value) {
            for ($i = 0; $i < $num; $i++) {
                if ($data[$i]['user_id'] == $value) {
                    $data_format[$key]['user_id'] = $data[$i]['user_id'];
                    $data_format[$key]['login'] = $data[$i]['login'];
                    $data_format[$key]['car_no'][] = $data[$i]['car_no'];
                }
            }
        }
        return $data_format;
    }

Is there any way to check it directly from the database? In addition, the login here needs to be queried from the user table. In the vehicle table, only user_id is associated with the user table. It needs to be paginated after making it. I plan to use array_chunk to split the array. Please give me some advice from the experts.

The complete code is as follows:

    /**
     * 獲取完整或者查詢用戶信息接口
     * @desc 后臺查詢用戶信息,可以根據(jù)登錄名,手機(jī)號,郵箱號,車牌號等查詢,不傳查詢參數(shù)表示獲取全部用戶信息
     * @param search string N N 需要搜索的條件,可以是登錄名,手機(jī)號,郵箱號,車牌號,例如:123,不傳參數(shù)表示獲取全部
     * @return int code 操作碼,200表示成功,其他表示沒有查到數(shù)據(jù)
     * @return string data.login 登錄名,是二維數(shù)組下標(biāo)
     * @return string data.car_no 車牌號,是二維數(shù)組下標(biāo)
     */
    public function searchUserInfo() {
        $search = $this->input->post('search');
        //如果沒有傳查詢參數(shù),就查詢?nèi)浚⒎猪?        if (empty($search)) {
            $pageSize = (int) $this->input->post('page_size') > 0 ? (int) $this->input->post('page_size') : 100;
            $pageNo = (int) $this->input->post('pageNo') > 0 ? (int) $this->input->post('pageNo') : 1;
            $field = 'id, user_id, car_no';
            $where = array(
                'id>' => 0,
            );
            $result = $this->CarManageModel->my_listPager($this->CarManageModel->table, $where, $field, $pageNo, $pageSize, '', 'user_id ASC', '', 'user_id');
            // $query=$this->db->last_query();
            // $this->outPutJson(200, array($query));
            echo "<pre>";
            print_r($result);
            echo "</pre>";
            exit;
            $this->outPutJson(200, $result);
        }

        // 如果傳參了,就根據(jù)電話號碼或者車牌號碼查詢
        //根據(jù)電話號碼查詢
        $this->db->select('id');
        $this->db->like('mobile', $search);
        $result_1 = $this->db->get('user')->result_array();
        $result_1 = array_column($result_1, 'id');

        //根據(jù)車牌號碼查詢
        $this->db->select('user_id');
        $this->db->like('car_no', $search);
        $result_2 = $this->db->get('car_info')->result_array();
        $result_2 = array_column($result_2, 'user_id');
        $result = array_merge($result_1, $result_2);

        if (!empty($result)) {
            $this->db->select('user_id,login,car_no');
            $this->db->from('car_info');
            $this->db->join('user', 'car_info.user_id = user.id', 'left');
            $this->db->where_in('car_info.user_id', $result);
            $data = $this->db->get()->result_array();

            $num = count($data);
            $data_user_id = array_unique(array_column($data, 'user_id'));
            $data_format = $this->group_sort($data_user_id, $num, $data);
            echo "<pre>";
            print_r($data_format);
            echo "</pre>";
            exit;
            if ($data_format) {
                $this->outPutJson(200, $data_format);
            }
            $this->outputJson(500);
        }
    }

    private function group_sort($unique, $num, $data) {
        foreach ($unique as $key => $value) {
            for ($i = 0; $i < $num; $i++) {
                if ($data[$i]['user_id'] == $value) {
                    $data_format[$key]['user_id'] = $data[$i]['user_id'];
                    $data_format[$key]['login'] = $data[$i]['login'];
                    $data_format[$key]['car_no'][] = $data[$i]['car_no'];
                }
            }
        }
        return $data_format;
    }

The database table structure is as follows:
Vehicle information table

CREATE TABLE `car_info` (
  `id` bigint(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
  `user_id` bigint(11) unsigned NOT NULL DEFAULT '0' COMMENT '后臺用戶id',
  `car_type_id` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '車型id,從第三方庫獲取',
  `car_no_type` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '車牌號碼類型,粵A粵B等',
  `car_no` varchar(10) NOT NULL DEFAULT '' COMMENT '車牌號碼',
  `frame_no` varchar(8) NOT NULL DEFAULT '' COMMENT '車架號碼后6位',
  `engine_no` varchar(8) NOT NULL DEFAULT '' COMMENT '發(fā)動機(jī)號后6位',
  `remark` varchar(200) NOT NULL DEFAULT '' COMMENT '車輛備注',
  `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否啟用',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創(chuàng)建時(shí)間',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時(shí)間',
  `is_deleted` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '軟刪除',
  PRIMARY KEY (`id`),
  KEY `idx_user_id` (`user_id`),
  KEY `idx_status` (`status`),
  KEY `idx_is_deleted` (`is_deleted`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8 COMMENT='用戶車輛信息,一個(gè)用戶可添加多個(gè)';

user table:

CREATE TABLE `user` (
  `id` bigint(11) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
  `login` varchar(50) NOT NULL DEFAULT '' COMMENT '用于登錄,用戶名/手機(jī)號/郵件',
  `password` varchar(50) NOT NULL DEFAULT '' COMMENT '登錄密碼',
  `nickname` varchar(50) NOT NULL DEFAULT '' COMMENT '昵稱或者顯示的用戶名',
  `email` varchar(50) NOT NULL DEFAULT '' COMMENT '郵件地址',
  `mobile` varchar(20) NOT NULL DEFAULT '' COMMENT '手機(jī)號碼',
  `reg_type` tinyint(1) NOT NULL DEFAULT '0' COMMENT '注冊類型,1為手機(jī),2為微信,3為email',
  `status` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否啟用',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '創(chuàng)建時(shí)間',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新時(shí)間',
  `is_deleted` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '軟刪除',
  PRIMARY KEY (`id`),
  KEY `idx_login` (`login`),
  KEY `idx_mobile` (`mobile`),
  KEY `idx_status` (`status`),
  KEY `idx_is_deleted` (`is_deleted`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COMMENT='用戶表';
ringa_lee
ringa_lee

ringa_lee

reply all(1)
學(xué)霸

SELECT user_id,group_concat(car_no) FROM 12311111 group by user_id

It seems more troublesome to operate based on the basics

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template