Как сгладить рекурсивный многомерный массив php.
Я пробовал с этим ссылка на сайт код, но не работает. Я пробовал много других ссылок, но не работает. это код был использован для создания этого массива.
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] => [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
)
Я пытался с приведенным ниже кодом, но он работает только для уровня 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] => [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
)
Позвольте мне знать, если вам нужно что-нибудь еще.
Я не знаю, почему вам нужно сделать это сложное флаттинг, и я уверен, что вы можете —as it's seems that you are fetching data from database
— что вы можете сделать это проще, чем следующим образом, однако вам может понадобиться использовать здесь рекурсию, например:
// 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));
Живой образец: https://3v4l.org/Rtn5f
P.S часто рекурсия дороже с большими массивами
Других решений пока нет …