如何将多维数组分配给Bidimensional Array? 在下拉列表中显示类别层次结构。($ form_multiselect())
我有这张桌子
| id | parent | name |
|----|--------|---------|
| 10 | 0 | 'menu1' |
| 12 | 10 | 'menu2' |
| 13 | 10 | 'menu3' |
| 14 | 0 | 'menu4' |
php代码是。
function prepare_items($data_from_table_of_database,$parent){
$items = array ();
foreach ( $data_from_table_of_database as $item ) {
if ($item ['parent'] == $parent) {
$items ['id'] = $item;
$items ['id'] ['children'] =
$this->prepare_items ( $data_from_table_of_database, $item ['id']);
}
}
}
我的数组代码如下所示
Array (
[107] => Array ( [id] => 107 [menu_title] => تست1 [parent] => 0 [sort_order] => 1 [children] => Array ( ) )
[111] => Array ( [id] => 111 [menu_title] => تست 4 [parent] => 0 [sort_order] => 1 [children] => Array (
[109] => Array ( [id] => 109 [menu_title] => تست 1 [parent] => 111 [sort_order] => 1 [children] => Array ( ) )
[115] => Array ( [id] => 115 [menu_title] => تست7 [parent] => 111 [sort_order] => 1 [children] => Array ( [110] => Array ( [id] => 110 [menu_title] => تست 3 [parent] => 115 [sort_order] => 1 [children] => Array ( ) ) ) ) ) )
[112] => Array ( [id] => 112 [menu_title] => تست 4 [parent] => 0 [sort_order] => 1 [children] => Array ( ) )
[113] => Array ( [id] => 113 [menu_title] => تست 5 [parent] => 0 [sort_order] => 1 [children] => Array ( ) )
[114] => Array ( [id] => 114 [menu_title] => تست شش6 [parent] => 0 [sort_order] => 3 [children] => Array (
[108] => Array ( [id] => 108 [menu_title] => تست 2 [parent] => 114 [sort_order] => 1 [children] => Array ( ) )
[118] => Array ( [id] => 118 [menu_title] => yeeeeeee [parent] => 114 [sort_order] => 1 [children] => Array ( ) ) ) )
[119] => Array ( [id] => 119 [menu_title] => ff [parent] => 0 [sort_order] => 1 [children] => Array (
[117] => Array ( [id] => 117 [menu_title] => shar [parent] => 119 [sort_order] => 1 [children] => Array ( ) ) ) )
)
我将添加另一个数组相同的结构
Array (
[107] => Array ( [id] => 107 [menu_title] => تست1 [sort_order] => 2 )
[111] => Array ( [id] => 111 [menu_title] => تست 4 [sort_order] => 3 )
[109] => Array ( [id] => 109 [menu_title] => تست 4 -- تست 1 [sort_order] => 1 )
[115] => Array ( [id] => 115 [menu_title] => تست 4 -- تست7 [sort_order] => 1 )
[110] => Array ( [id] => 110 [menu_title] => تست 4 -- تست7 -- تست 3 [sort_order] => 1)
.
.
.
)
我昨天提出另一个问题涉及此主题。
我正在尝试从$ form-> multiselect()创建一个多级别类别。
答案 0 :(得分:0)
我找到了答案。
private function build_item(&$list,$item,$level,$element_keys){
$list[$item[$element_keys['menu_id']]]=str_repeat(" -> ", $level). $item[$element_keys['menu_title']];
}
private function build_item_list($items,$element_keys ,$element_keys,$level=0) {
foreach ( $items as $item ) {
$this->build_item($this->list,$item,$level,$element_keys);
if(count($item['children'])>0){
$this->build_item_list($item['children'],$element_keys,$element_keys,++$level);
}
}
return ( $this->list );
}
它在
下面生成数组 [1] => Array ( [107] => تست1 )
[2] => Array ( [111] => تست 4 )
[3] => Array ( [109] => -> تست 1 )
[4] => Array ( [115] => -> تست7 )
[5] => Array ( [110] => -> -> تست 3 )
[6] => Array ( [112] => -> تست 4 )
[7] => Array ( [113] => -> تست 5 )
[8] => Array ( [114] => -> تست شش6 )
[9] => Array ( [108] => -> -> تست 2 )
这是我的天堂。