1

How to flatten multidimensional array recursive php. I tried with this link code but not working. I tried many other links also but not working. This code has been used for creating this array.

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;
}

Here is the array which I need to flatten.

Array
(
    [0] => stdClass Object
        (
            [id] => 1135
            [gid] => 4
            [parent_id] => 1112
            [username] => sdafasdf
            [email] => [email protected]
            [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] => [email protected]
            [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] => [email protected]
            [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] => [email protected]
                            [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] => [email protected]
                                            [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] => [email protected]
                            [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] => [email protected]
            [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
        )

I tried with the below code but it working for only level 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;
}

Desire output

Array
(
    [0] => stdClass Object
        (
            [id] => 1135
            [gid] => 4
            [parent_id] => 1112
            [username] => sdafasdf
            [email] => [email protected]
            [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] => [email protected]
            [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] => [email protected]
            [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] => [email protected]
            [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] => [email protected]
            [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] => [email protected]
                [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
            )

Let me know if you need anything else.

2
  • what's the expected result ? Commented Apr 25, 2017 at 11:58
  • @hassan check the updated question Commented Apr 25, 2017 at 12:06

1 Answer 1

3

I don't know why you need to do this complicated flatting , and I'm pretty sure that you can -as it's seems that you are fetching data from database- that you can accomplish this easier that the following way, However you may need to use recursion here, for example :

// 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));

Live sample : https://3v4l.org/Rtn5f

P.S often recursion is more expensive with big arrays

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.