【发布时间】:2016-02-19 07:27:40
【问题描述】:
在不同论坛上进行了太多搜索后,我决定寻求帮助。我正在为
构建数据数组美国 ->
电视频道 ->
戏剧 ->
剧集
如您所见,有四个级别。我正在使用此代码来开发它
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index() {
$arr = array(
array(
'id' => 100,
'dramaID' => 0,
'Country name' => 'America'
),
array(
'id' => 101,
'dramaID' => 100,
'Channels' => 'Tv Channels',
'url' => ''
),
array(
'id' => 102,
'dramaID' => 101,
'Dramaname' => 'Taken',
'url' => ''
),
array(
'id' => 103,
'dramaID' => 102,
'Episodename' => 'Episode 1',
'url' => ''
)
);
// 匹配id 到dramaID 的项目嵌套在children 数组中
$results = $this->array_tree($arr, 'id', 'dramaID', 'channels');
echo"<pre>";
print_r($results);
}
function array_tree($arr, $main_index, $parent_index, $child_index) {
$new = array();
foreach ($arr as $a) {
$new[$a[$parent_index]][] = $a;
}
// we create a closure in order to be recursive
function create_tree(&$list, $parent, $i, $c) {
$tree = array();
foreach ($parent as $k => $l) {
if (isset($list[$l[$i]])) {
$l[$c] = create_tree($list, $list[$l[$i]], $i, $c);
}
$tree[] = $l;
}
return $tree;
}
return create_tree($new, $new[0], $main_index, $child_index);
}
请帮忙。我需要在代码中做哪些更改才能传递键名?
【问题讨论】:
标签: php mysql arrays codeigniter