flatten multidimensional数组递归php

时间:2017-04-25 09:46:20

标签: php arrays codeigniter multidimensional-array flatten

如何压缩多维数组递归php。 我试过这个link代码但没有用。我尝试了许多其他链接,但没有工作。 This代码已用于创建此数组。

public function downline_income($userId = null, $offset = 0) {
    $userId = user::id();
    $limit = AZ::setting('record_per_page');
    $objUser = new stdClass();
    $objUser->id = $userId;
    $downline = $this->user->getDownline($objUser);

    AZ::layout('left-content', array(
        'block' => 'account/downline_income',
        'user' => $userId,
        'total_users' => $total_users,
        'pagination' => $pagination,
        'q' => $userId,
        'data' => $downline,
        'offset' => $offset,
    ));
}

public function getDownline($obj, $level = 0) {
    $obj->level = $level;

    $where = array('parent_id' => $obj->id);
    $this->db->select('users.*');
    $this->db->where($where);

    $query = $this->db->get('users')->result();

    foreach ($query as $objUser) {
        $obj->data[] = $this->getDownline($objUser, ($level + 1));
    }

    return $obj;
}

这是我需要展平的阵列。

Array
(
    [0] => stdClass Object
        (
            [id] => 1135
            [gid] => 4
            [parent_id] => 1112
            [username] => sdafasdf
            [email] => kapisdafsdal@abc.com
            [name] => sdafsda
            [status] => 0
            [registerd] => 2017-04-19 15:44:38
            [last_login] => 0000-00-00 00:00:00
            [password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
            [tranjection_password] => 
            [package_id] => 5
            [user_id] => 1135
            [purchase_date] => 2017-04-19 15:44:39
            [confirm_date] => 0000-00-00 00:00:00
            [package_name] => USD 500
            [amount] => 500
            [daily_income] => 12
            [total_income] => 600
            [time_duration] => 60
            [level] => 1
        )

    [1] => stdClass Object
        (
            [id] => 1134
            [gid] => 4
            [parent_id] => 1112
            [username] => sdaf254
            [email] => asadfsad@abc.com
            [name] => categoryA
            [status] => 0
            [registerd] => 2017-04-19 15:36:11
            [last_login] => 0000-00-00 00:00:00
            [password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
            [tranjection_password] => 
            [package_id] => 4
            [user_id] => 1134
            [purchase_date] => 2017-04-20 00:00:00
            [confirm_date] => 0000-00-00 00:00:00
            [package_name] => USD 250
            [amount] => 250
            [daily_income] => 12
            [total_income] => 600
            [time_duration] => 60
            [level] => 1
        )

    [2] => stdClass Object
        (
            [id] => 1136
            [gid] => 4
            [parent_id] => 1112
            [username] => test
            [email] => shrasdaf@abc.com
            [name] => test
            [status] => 0
            [registerd] => 2017-04-20 08:49:25
            [last_login] => 2017-04-21 10:42:25
            [password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
            [tranjection_password] => 
            [package_id] => 7
            [user_id] => 1136
            [purchase_date] => 2017-04-20 08:49:25
            [confirm_date] => 0000-00-00 00:00:00
            [package_name] => USD 2500
            [amount] => 2500
            [daily_income] => 12
            [total_income] => 600
            [time_duration] => 60
            [level] => 1
            [data] => Array
                (
                    [0] => stdClass Object
                        (
                            [id] => 1148
                            [gid] => 4
                            [parent_id] => 1136
                            [username] => test_downline
                            [email] => kapilsdfasf@abc.com
                            [name] => test_downline
                            [status] => 0
                            [registerd] => 2017-04-21 10:42:56
                            [last_login] => 2017-04-21 11:08:00
                            [password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
                            [tranjection_password] => 
                            [package_id] => 9
                            [user_id] => 1148
                            [purchase_date] => 2017-04-21 10:42:56
                            [confirm_date] => 0000-00-00 00:00:00
                            [package_name] => USD 10000
                            [amount] => 10000
                            [daily_income] => 12
                            [total_income] => 600
                            [time_duration] => 60
                            [level] => 2
                            [data] => Array
                                (
                                    [0] => stdClass Object
                                        (
                                            [id] => 1150
                                            [gid] => 4
                                            [parent_id] => 1148
                                            [username] => test1_downline1
                                            [email] => kapil25@abc.com
                                            [name] => test1_downline1
                                            [status] => 0
                                            [registerd] => 2017-04-21 11:08:27
                                            [last_login] => 0000-00-00 00:00:00
                                            [password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
                                            [tranjection_password] => 
                                            [package_id] => 5
                                            [user_id] => 1150
                                            [purchase_date] => 2017-04-21 11:08:27
                                            [confirm_date] => 0000-00-00 00:00:00
                                            [package_name] => USD 500
                                            [amount] => 500
                                            [daily_income] => 12
                                            [total_income] => 600
                                            [time_duration] => 60
                                            [level] => 3
                                        )

                                )

                        )

                    [1] => stdClass Object
                        (
                            [id] => 1149
                            [gid] => 4
                            [parent_id] => 1136
                            [username] => test_downline2
                            [email] => kapil123@abc.com
                            [name] => test_downline2
                            [status] => 0
                            [registerd] => 2017-04-21 11:06:35
                            [last_login] => 0000-00-00 00:00:00
                            [password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
                            [tranjection_password] => 
                            [package_id] => 6
                            [user_id] => 1149
                            [purchase_date] => 2017-04-21 11:06:35
                            [confirm_date] => 0000-00-00 00:00:00
                            [package_name] => USD 1000
                            [amount] => 1000
                            [daily_income] => 12
                            [total_income] => 600
                            [time_duration] => 60
                            [level] => 2
                        )

                )

        )

    [3] => stdClass Object
        (
            [id] => 1137
            [gid] => 4
            [parent_id] => 1112
            [username] => test2
            [email] => ishrasdaf1@abc.com
            [name] => test 2
            [status] => 0
            [registerd] => 2017-04-20 08:54:59
            [last_login] => 0000-00-00 00:00:00
            [password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
            [tranjection_password] => 
            [package_id] => 6
            [user_id] => 1137
            [purchase_date] => 2017-04-20 08:55:00
            [confirm_date] => 0000-00-00 00:00:00
            [package_name] => USD 1000
            [amount] => 1000
            [daily_income] => 12
            [total_income] => 600
            [time_duration] => 60
            [level] => 1
        )

我尝试使用以下代码,但它仅适用于第2级

public function array_flatten($array, $i = 0) {
    $flat = array();

    foreach ($array as $value) {
        if (isset($value->data) && is_array($value->data)) {
            $flat = array_merge($flat, $this->array_flatten($value->data));
        } else {
            $flat[] = $value;
        }
    }
    return $flat;
}

欲望输出

Array
(
    [0] => stdClass Object
        (
            [id] => 1135
            [gid] => 4
            [parent_id] => 1112
            [username] => sdafasdf
            [email] => kapisdafsdal@abc.com
            [name] => sdafsda
            [status] => 0
            [registerd] => 2017-04-19 15:44:38
            [last_login] => 0000-00-00 00:00:00
            [password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
            [tranjection_password] => 
            [package_id] => 5
            [user_id] => 1135
            [purchase_date] => 2017-04-19 15:44:39
            [confirm_date] => 0000-00-00 00:00:00
            [package_name] => USD 500
            [amount] => 500
            [daily_income] => 12
            [total_income] => 600
            [time_duration] => 60
            [level] => 1
        )

    [1] => stdClass Object
        (
            [id] => 1134
            [gid] => 4
            [parent_id] => 1112
            [username] => sdaf254
            [email] => asadfsad@abc.com
            [name] => categoryA
            [status] => 0
            [registerd] => 2017-04-19 15:36:11
            [last_login] => 0000-00-00 00:00:00
            [password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
            [tranjection_password] => 
            [package_id] => 4
            [user_id] => 1134
            [purchase_date] => 2017-04-20 00:00:00
            [confirm_date] => 0000-00-00 00:00:00
            [package_name] => USD 250
            [amount] => 250
            [daily_income] => 12
            [total_income] => 600
            [time_duration] => 60
            [level] => 1
        )

    [2] => stdClass Object
        (
            [id] => 1136
            [gid] => 4
            [parent_id] => 1112
            [username] => test
            [email] => shrasdaf@abc.com
            [name] => test
            [status] => 0
            [registerd] => 2017-04-20 08:49:25
            [last_login] => 2017-04-21 10:42:25
            [password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
            [tranjection_password] => 
            [package_id] => 7
            [user_id] => 1136
            [purchase_date] => 2017-04-20 08:49:25
            [confirm_date] => 0000-00-00 00:00:00
            [package_name] => USD 2500
            [amount] => 2500
            [daily_income] => 12
            [total_income] => 600
            [time_duration] => 60
            [level] => 1
        )
    [3] => stdClass Object
        (
            [id] => 1148
            [gid] => 4
            [parent_id] => 1136
            [username] => test_downline
            [email] => kapilsdfasf@abc.com
            [name] => test_downline
            [status] => 0
            [registerd] => 2017-04-21 10:42:56
            [last_login] => 2017-04-21 11:08:00
            [password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
            [tranjection_password] => 
            [package_id] => 9
            [user_id] => 1148
            [purchase_date] => 2017-04-21 10:42:56
            [confirm_date] => 0000-00-00 00:00:00
            [package_name] => USD 10000
            [amount] => 10000
            [daily_income] => 12
            [total_income] => 600
            [time_duration] => 60
            [level] => 2
    )
    [4] => stdClass Object
        (
            [id] => 1150
            [gid] => 4
            [parent_id] => 1148
            [username] => test1_downline1
            [email] => kapil25@abc.com
            [name] => test1_downline1
            [status] => 0
            [registerd] => 2017-04-21 11:08:27
            [last_login] => 0000-00-00 00:00:00
            [password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
            [tranjection_password] => 
            [package_id] => 5
            [user_id] => 1150
            [purchase_date] => 2017-04-21 11:08:27
            [confirm_date] => 0000-00-00 00:00:00
            [package_name] => USD 500
            [amount] => 500
            [daily_income] => 12
            [total_income] => 600
            [time_duration] => 60
            [level] => 3
        )

    [5] => stdClass Object
        (
                [id] => 1149
                [gid] => 4
                [parent_id] => 1136
                [username] => test_downline2
                [email] => kapil123@abc.com
                [name] => test_downline2
                [status] => 0
                [registerd] => 2017-04-21 11:06:35
                [last_login] => 0000-00-00 00:00:00
                [password] => 4eca045dfa240f56a1f9d45eaa53b71c6eccd6a7
                [tranjection_password] => 
                [package_id] => 6
                [user_id] => 1149
                [purchase_date] => 2017-04-21 11:06:35
                [confirm_date] => 0000-00-00 00:00:00
                [package_name] => USD 1000
                [amount] => 1000
                [daily_income] => 12
                [total_income] => 600
                [time_duration] => 60
                [level] => 2
            )

如果您还有其他需要,请告诉我。

1 个答案:

答案 0 :(得分:2)

我不知道为什么你需要做这种复杂的扁平化,我很确定你可以 - // Sample data $arr = [ ['level' => 1, 'data' => [['level' => 2],['level' => 3, 'data' => [['level' => 6]]]]], ['level' => 4], ['level' => 9, 'data' => [['level' => 8]]], ]; function flat($data, &$list = []) { foreach ($data as $key => $value) { if (isset($value['data']) && is_array($value['data'])) { $tmp = $value['data']; unset($value['data']); flat($tmp, $list); } $list[] = $value; } return $list; } print_r(flat($arr)); - 你可以通过以下方式更轻松地完成这项工作,但是你可能需要在这里使用递归,例如:

npm install

实时样本:https://3v4l.org/Rtn5f

P.S 对于大数组而言,递归通常更为昂贵