Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从多维php数组构建父子类别面包屑_Php_Multidimensional Array_Codeigniter 3 - Fatal编程技术网

从多维php数组构建父子类别面包屑

从多维php数组构建父子类别面包屑,php,multidimensional-array,codeigniter-3,Php,Multidimensional Array,Codeigniter 3,帮助我构建一个动态函数来显示任意数量的子类别。到目前为止,只有三个级别的类别。请指导我构建一个代码来处理单亲的任意数量的子类别,并将其显示为面包屑格式 下面是codeigniter索引函数,它从categories表中获取所有类别 public function index() { $dbdata['table'] = 'categories'; $dbdata['select'] = array('id', 'parent', 'name'); $dbdata['whe

帮助我构建一个动态函数来显示任意数量的子类别。到目前为止,只有三个级别的类别。请指导我构建一个代码来处理单亲的任意数量的子类别,并将其显示为面包屑格式

下面是codeigniter索引函数,它从categories表中获取所有类别

public function index() {
    $dbdata['table'] = 'categories';
    $dbdata['select'] = array('id', 'parent', 'name');
    $dbdata['where'] = array('type' => 'post');
    $records = $this->ot->get_records($dbdata);
    $category = array();
    foreach ($records as $record) {
        if ($record['parent']) {
            $parent = $this->get_parent($record['parent'], $records);
            if ($parent['parent']) {
                $super_parent = $this->get_parent($parent, $records);
                $category[] = $super_parent['name'] . ' > ' . $parent['name'] . ' > ' . $record['name'];
            } else {
                $category[] = $parent['name'] . ' > ' . $record['name'];
            }
        } else {
            $category[] = $record['name'];
        }
    }
    echo '
<pre>';
    print_r($records);
    echo '<hr>';
    print_r($category);
    echo '</pre>';
}

protected function get_parent($parent, $records) {
    $key = array_search($parent, array_column($records, 'id'));
    return $records[$key];
}
试试这个代码

公共功能索引(){
$dbdata['table']='categories';
$dbdata['select']=数组('id','parent','name');
$dbdata['where']=array('type'=>'post');
$records=$this->ot->get_记录($dbdata);
对于($i=0;$icategory($records,0,$records[$i]);
}
}
函数类别($records,$parent=0,$record=NULL){
如果($记录){
$parent='';
if($record['parent'])){
$parent=$this->category($records,$record['parent']);
}
返回$parent.$record['name'];
}否则{
$super_parent='';
$key=array_search($parent,array_column($records,'id'));
if($records[$key]['parent'])){
$super_parent=$this->category($records,$records[$key]['parent']);
}
$super_parent.=$records[$key]['name']./';
返回$super_parent;
}
}

“请提供php代码…”抱歉,不,这不是免费的编码服务,如果您自己编写,即使它不起作用,我们也会尽力提供帮助
Array
(
    [0] => Array
    (
        [id] => 1
        [parent] => 0
        [name] => Category 1
    )
    [1] => Array
    (
        [id] => 2
        [parent] => 1
        [name] => Category 2
    )
    [2] => Array
    (
        [id] => 3
        [parent] => 2
        [name] => Category 3
    )
    [3] => Array
    (
        [id] => 4
        [parent] => 0
        [name] => Category 4
    )
    [4] => Array
    (
        [id] => 5
        [parent] => 4
        [name] => Category 5
    )
    [5] => Array
    (
        [id] => 6
        [parent] => 0
        [name] => Category 6
    )
    [6] => Array
    (
        [id] => 7
        [parent] => 0
        [name] => Category 7
    )
    [7] => Array
    (
        [id] => 8
        [parent] => 0
        [name] => Category 8
    )
    [8] => Array
    (
        [id] => 9
        [parent] => 0
        [name] => Category 9
    )
    [9] => Array
    (
        [id] => 10
        [parent] => 0
        [name] => Category 10
    )
    [10] => Array
    (
        [id] => 11
        [parent] => 0
        [name] => Category 11
    )
    [11] => Array
    (
        [id] => 12
        [parent] => 0
        [name] => Category 12
    )
)
Array
(
    [0] => Category 1
    [1] => Category 1 > Category 2
    [2] => Category 1 > Category 2 > Category 3
    [3] => Category 4
    [4] => Category 4 > Category 5
    [5] => Category 6
    [6] => Category 7
    [7] => Category 8
    [8] => Category 9
    [9] => Category 10
    [10] => Category 11
    [11] => Category 12
)
public function index() {
            $dbdata['table'] = 'categories';
            $dbdata['select'] = array('id', 'parent', 'name');
            $dbdata['where'] = array('type' => 'post');
            $records = $this->ot->get_records($dbdata);

            for ($i = 0; $i < sizeof($records); $i++) {
                $records[$i]['category'] = $this->category($records, 0, $records[$i]);
            }
        }

        function category($records, $parent = 0, $record = NULL) {
            if ($record) {
                $parent = '';
                if ($record['parent']) {
                    $parent = $this->category($records, $record['parent']);
                }
                return $parent . $record['name'];
            } else {
                $super_parent = '';
                $key = array_search($parent, array_column($records, 'id'));
                if ($records[$key]['parent']) {
                    $super_parent = $this->category($records, $records[$key]['parent']);
                }
                $super_parent .= $records[$key]['name'] . ' / ';
                return $super_parent;
            }
        }