Rumah > Soal Jawab > teks badan
Array
(
[1] => Array
(
[id] => 1
[access_id] => 1
[name] => 统计报表
[module] => manage
[parent_id] => 0
[icon] => fa-dashboard
[type] => menu
[sort] => 0
[status] => 1
[access_name] => 统计报表
[controller] => dashboard
[action] => index
)
[2] => Array
(
[id] => 2
[access_id] =>
[name] => 个人中心
[module] => manage
[parent_id] => 0
[icon] => fa-male
[type] => menu
[sort] => 1
[status] => 1
[access_name] =>
[controller] =>
[action] =>
[_child] => Array
(
[3] => Array
(
[id] => 3
[access_id] =>
[name] => 修改个人信息
[module] => manage
[parent_id] => 2
[icon] =>
[type] => menu
[sort] => 0
[status] => 1
[access_name] =>
[controller] =>
[action] =>
)
)
)
[4] => Array
(
[id] => 4
[access_id] =>
[name] => 广告管理
[module] => manage
[parent_id] => 0
[icon] => fa-th
[type] => menu
[sort] => 2
[status] => 1
[access_name] =>
[controller] =>
[action] =>
[_child] => Array
(
[5] => Array
(
[id] => 5
[access_id] =>
[name] => 广告位管理
[module] => manage
[parent_id] => 4
[icon] =>
[type] => menu
[sort] => 0
[status] => 1
[access_name] =>
[controller] =>
[action] =>
[_child] => Array
(
[8] => Array
(
[id] => 8
[access_id] =>
[name] => 添加广告位
[module] => manage
[parent_id] => 5
[icon] =>
[type] => menu
[sort] => 1
[status] => 1
[access_name] =>
[controller] =>
[action] =>
)
[7] => Array
(
[id] => 7
[access_id] =>
[name] => 广告位列表
[module] => manage
[parent_id] => 5
[icon] =>
[type] => menu
[sort] => 2
[status] => 1
[access_name] =>
[controller] =>
[action] =>
)
)
)
[6] => Array
(
[id] => 6
[access_id] =>
[name] => 广告内容管理
[module] => manage
[parent_id] => 4
[icon] =>
[type] => menu
[sort] => 1
[status] => 1
[access_name] =>
[controller] =>
[action] =>
[_child] => Array
(
[9] => Array
(
[id] => 9
[access_id] =>
[name] => 广告列表
[module] => manage
[parent_id] => 6
[icon] =>
[type] => menu
[sort] => 0
[status] => 1
[access_name] =>
[controller] =>
[action] =>
)
[10] => Array
(
[id] => 10
[access_id] =>
[name] => 添加广告
[module] => manage
[parent_id] => 6
[icon] =>
[type] => menu
[sort] => 1
[status] => 1
[access_name] =>
[controller] =>
[action] =>
)
)
)
)
)
)
Di atas ialah tatasusunan, yang mengandungi maklumat menu Ia mungkin mempunyai banyak dimensi Sekarang ada soalan Jika id menu ialah 8, iaitu, indeksnya ialah $arr[4]['_child'][5]['_child'][8]
. . Bagaimanakah saya boleh menulis menu ini? Fungsi mengembalikan tatasusunan yang mengandungi semua nilai indeks Contohnya, menu dengan id 8 mengembalikan [4,5,8]
.
过去多啦不再A梦2017-05-16 13:00:11
_Child array Hanya tambahkan id menu induk pada setiap tatasusunan, dan kemudian baca lapisan demi lapisan melalui indeks, jika tidak, anda perlu untuk gelung untuk menilai tatasusunan demi tatasusunan. Kesannya terlalu teruk
世界只因有你2017-05-16 13:00:11
function EachTree(json, id) {
var path = [];
for (var i = 0; i < json.length; i++) {
var arr = json[i], _id = arr["id"];
path.push(_id);
if (_id == id) {
return path;
}
if (arr["_child"] && arguments.callee(arr["_child"], id)) {
return path;
}
}
}
console.log(EachTree(json, 8));