要实现的效果: Array( [2023---年份] = Array ( [1--月份:] = Array ( [0] = Array ( [id] = 28 [billdtimes] = 1672502400 [month] = 1 [price] = 8.00 [years] = 2023 [wuyeId] = 1 [communityId] = 3 [unitNumber] = 2 [buildingId] = 1 [ro
要实现的效果:
Array
(
[2023---年份] => Array
(
[1--月份:] => Array
(
[0] => Array
(
[id] => 28
[billdtimes] => 1672502400
[month] => 1
[price] => 8.00
[years] => 2023
[wuyeId] => 1
[communityId] => 3
[unitNumber] => 2
[buildingId] => 1
[roomNumber] => 302
[billStartime] => 1672502400
[billEndtime] => 1675094400
)
[1] => Array
(
[id] => 31
[billdtimes] => 1672502400
[month] => 1
[price] => 3.00
[years] => 2023
[wuyeId] => 1
[communityId] => 3
[unitNumber] => 2
[buildingId] => 1
[roomNumber] => 302
[billStartime] => 1672502400
[billEndtime] => 1675094400
)
)
[2] => Array
(
[0] => Array
(
[id] => 29
[billdtimes] => 1675180800
[month] => 2
[price] => 8.00
[years] => 2023
[wuyeId] => 1
[communityId] => 3
[unitNumber] => 2
[buildingId] => 1
[roomNumber] => 302
[billStartime] => 1675180800
[billEndtime] => 1677513600
)
)
[3] => Array
(
[0] => Array
(
[id] => 30
[billdtimes] => 1677600000
[month] => 3
[price] => 8.00
[years] => 2023
[wuyeId] => 1
[communityId] => 3
[unitNumber] => 2
[buildingId] => 1
[roomNumber] => 302
[billStartime] => 1677600000
[billEndtime] => 1680192000
)
)
[4] => Array
(
[0] => Array
(
[id] => 6
[billdtimes] => 1680313031
[month] => 4
[price] => 6.00
[years] => 2023
[wuyeId] => 1
[communityId] => 3
[unitNumber] => 2
[buildingId] => 1
[roomNumber] => 302
[billStartime] => 0
[billEndtime] => 0
)
[1] => Array
(
[id] => 7
[billdtimes] => 1680313031
[month] => 4
[price] => 1.00
[years] => 2023
[wuyeId] => 1
[communityId] => 3
[unitNumber] => 2
[buildingId] => 1
[roomNumber] => 302
[billStartime] => 0
[billEndtime] => 0
)
[2] => Array
(
[id] => 8
[billdtimes] => 1680313031
[month] => 4
[price] => 2.00
[years] => 2023
[wuyeId] => 1
[communityId] => 3
[unitNumber] => 2
[buildingId] => 1
[roomNumber] => 302
[billStartime] => 0
[billEndtime] => 0
)
)
)
[2022] => Array
(
[12] => Array
(
[0] => Array
(
[id] => 11
[billdtimes] => 1669859617
[month] => 12
[price] => 6.00
[years] => 2022
[wuyeId] => 1
[communityId] => 3
[unitNumber] => 2
[buildingId] => 1
[roomNumber] => 302
[billStartime] => 1669859617
[billEndtime] => 1672365217
)
)
)
)
select代码数据
Array
(
[0] => Array
(
[id] => 28
[billdtimes] => 1672502400
[month] => 1
[price] => 8.00
[years] => 2023
[wuyeId] => 1
[communityId] => 3
[unitNumber] => 2
[buildingId] => 1
[roomNumber] => 302
[billStartime] => 1672502400
[billEndtime] => 1675094400
)
[1] => Array
(
[id] => 31
[billdtimes] => 1672502400
[month] => 1
[price] => 3.00
[years] => 2023
[wuyeId] => 1
[communityId] => 3
[unitNumber] => 2
[buildingId] => 1
[roomNumber] => 302
[billStartime] => 1672502400
[billEndtime] => 1675094400
)
[2] => Array
(
[id] => 29
[billdtimes] => 1675180800
[month] => 2
[price] => 8.00
[years] => 2023
[wuyeId] => 1
[communityId] => 3
[unitNumber] => 2
[buildingId] => 1
[roomNumber] => 302
[billStartime] => 1675180800
[billEndtime] => 1677513600
)
[3] => Array
(
[id] => 30
[billdtimes] => 1677600000
[month] => 3
[price] => 8.00
[years] => 2023
[wuyeId] => 1
[communityId] => 3
[unitNumber] => 2
[buildingId] => 1
[roomNumber] => 302
[billStartime] => 1677600000
[billEndtime] => 1680192000
)
[4] => Array
(
[id] => 6
[billdtimes] => 1680313031
[month] => 4
[price] => 6.00
[years] => 2023
[wuyeId] => 1
[communityId] => 3
[unitNumber] => 2
[buildingId] => 1
[roomNumber] => 302
[billStartime] => 0
[billEndtime] => 0
)
[5] => Array
(
[id] => 7
[billdtimes] => 1680313031
[month] => 4
[price] => 1.00
[years] => 2023
[wuyeId] => 1
[communityId] => 3
[unitNumber] => 2
[buildingId] => 1
[roomNumber] => 302
[billStartime] => 0
[billEndtime] => 0
)
[6] => Array
(
[id] => 8
[billdtimes] => 1680313031
[month] => 4
[price] => 2.00
[years] => 2023
[wuyeId] => 1
[communityId] => 3
[unitNumber] => 2
[buildingId] => 1
[roomNumber] => 302
[billStartime] => 0
[billEndtime] => 0
)
[7] => Array
(
[id] => 11
[billdtimes] => 1669859617
[month] => 12
[price] => 6.00
[years] => 2022
[wuyeId] => 1
[communityId] => 3
[unitNumber] => 2
[buildingId] => 1
[roomNumber] => 302
[billStartime] => 1669859617
[billEndtime] => 1672365217
)
)
实现代码:
sonlist 查询的元数据 使用&引用方法实现
// 创建基于主键的数组引用
$refer = array();
foreach ($sonlist as $key => $data) {
$refer[$data["years"]][$data["month"]][] = &$sonlist[$key];
}
public function getFormatMenus($map = [], $field = '*')
{
$list = $this->field($field)->where($map)->select()->order('sort asc')->toArray();
return $list ? $list : [];
}
public function listToTree(array $list)
{
$tree = array();
$pk = 'id';
$pid = 'pid';
$root = 0;
$child = 'children';
if (is_array($list)) {
// 创建基于主键的数组引用
$refer = array();
foreach ($list as $key => $data) {
$refer[$data[$pk]] = &$list[$key];
}
foreach ($list as $key => $data) {
// 判断是否存在parent
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] = &$list[$key];
} else {
if (isset($refer[$parentId])) {
$parent = &$refer[$parentId];
$parent['childs'][] = $data['id'];
$parent[$child][] = &$list[$key];
}
}
}
}
return $tree;
}