doucan8246326 2013-06-15 20:50
浏览 63

如何在数组中的父级内获取返回的子级?

I have to PHP functions that returns all childs of a parent element recursively but, when I try to get the returned child inside its parent in the array I receive errors or the child is just been added in the root level in the array. How can I modify this functions to insert the child element inside its parent element in the children array ??

public function getAllChilds($Parent_ID, $level_identifier="", $start=true) { // get     all the childs of all the levels under a parent as a tree      
        $immediate_childs=$this->getImmediateChilds($Parent_ID,  $this-    >extra_condition, $this->order_by_phrase);
        if(count($immediate_childs)) {
            foreach($immediate_childs as $chld) {
                $chld[$this->item_list_field_name]=$level_identifier.$this->item_pointer.$chld[$this->item_list_field_name];
                array_push($this->all_childs,$chld);
                $this->getAllChilds($chld[$this->item_identifier_field_name], ($level_identifier.$this->level_identifier), false);
            }
        }
        if($start) {
            return $this->all_childs; 
        }
    } 

private function getImmediateChilds($parent_identifier_field_value, $extra_condition="", $order_by_phrase="") { // get only the direct/immediate childs under a parent 
    $sql="SELECT * FROM `".$this->db_table."` WHERE `".$this->parent_identifier_field_name."`='".$parent_identifier_field_value."' ".$extra_condition." ".$order_by_phrase;
    $res=mysql_query($sql);
    $childs=array();
    while($val=mysql_fetch_assoc($res)) {
        array_push($childs,$val);
    }
    return $childs; 
}

This line is the one that insert the child inside the array, but I want the child to be inserted inside its parent adding another array element called children which is the target array inside the parent where to place the child returned element..

array_push($this->all_childs,$chld);

Please help!! The object of this function is to finally return a multilevel array tree which will be transformed in a JSON tree.

thanks

this is the full class code:

<?php   
class ParentChild { 

    //properties which hold database and table related information  : start     
    var $db_host;
    var $db_user;
    var $db_pass;
    var $db_database;
    var $db_table; 


    var $item_identifier_field_name;  //may be the primary key of the table : as decided by the db designer 
    var $parent_identifier_field_name; //the fileld name in the table which holds the value of the item_identifier_field_name any item's parent : as decided by the db designer
    var $item_list_field_name; //field name in the table whose value will be shown in the list or tree (like name or any id etc.) : as choosen by the programmer 

    var $extra_condition="";  //if any extra condition should be added with the query : as desided by the programmer 
    var $order_by_phrase="";  //if any order by phrase should be added with the query : as desided by the programmer 
    //properties which hold database and table related information  : end



    var $level_identifier = "  ";  //no. of level of any item as per the generated tree : it will appear number of level times before the item in the list/tree
    var $item_pointer = "|-"; 



    var $all_childs = array(); //contains the entire tree or list starting from a given root element

    var $item_path = array(); //contains the total path of a given element/node(the list of elements starting from the top level root node to the given element/node)

    public function getAllChilds($Parent_ID, $level_identifier="", $start=true) { // get all the childs of all the levels under a parent as a tree      
        $immediate_childs=$this->getImmediateChilds($Parent_ID,  $this->extra_condition, $this->order_by_phrase);
        if(count($immediate_childs)) {
            foreach($immediate_childs as $chld) {
                $chld[$this->item_list_field_name]=$level_identifier.$this->item_pointer.$chld[$this->item_list_field_name];
                array_push($this->all_childs,$chld);
                $this->getAllChilds($chld[$this->item_identifier_field_name], ($level_identifier.$this->level_identifier), false);
            }
        }
        if($start) {
            return $this->all_childs; 
        }
    } 

    private function getImmediateChilds($parent_identifier_field_value, $extra_condition="", $order_by_phrase="") { // get only the direct/immediate childs under a parent 
        $sql="SELECT * FROM `".$this->db_table."` WHERE `".$this->parent_identifier_field_name."`='".$parent_identifier_field_value."' ".$extra_condition." ".$order_by_phrase;
        $res=mysql_query($sql);
        $childs=array();
        while($val=mysql_fetch_assoc($res)) {
            array_push($childs,$val);
        }
        return $childs; 
    }

    public function getItemPath($item_id,$start=true){ //returns the total path of a given item/node(the list of elements starting from the top level root node to the given item/node)

        if($item_id != 0) {
            $sql="SELECT * FROM `".$this->db_table."` WHERE `".$this->item_identifier_field_name."`='".$item_id."' ";
            $res=mysql_query($sql);
            $itemdata=mysql_fetch_assoc($res);
            array_push($this->item_path,$itemdata); 

            if($itemdata[$this->parent_identifier_field_name]!=0) {
                $this->item_path=$this->getItemPath($itemdata[$this->parent_identifier_field_name],false);
            } 
            if ($start) {
                $this->item_path=array_reverse($this->item_path);
            }
        }
        return $this->item_path;

    }

    public function db_connect(){
        $conn = mysql_connect($this->db_host, $this->db_user, $this->db_pass); 
        if($conn) {
            mysql_select_db($this->db_database, $conn);
        } 
        return $conn;
    }

    public function db_disconnect(){
        mysql_close();
    }
} 
?>
  • 写回答

1条回答 默认 最新

  • douningzhi1991 2013-06-16 01:57
    关注

    I found a function that reorder and build the nested required array from the raw produced array by that class:

    function makeParentChildRelations(&$inArray, &$outArray, $currentParentId = 0) {
            if(!is_array($inArray)) {
                return;
            }
    
            if(!is_array($outArray)) {
                return;
            }
    
            foreach($inArray as $key => $tuple) {
                if($tuple['parentID'] == $currentParentId) {
                    $tuple['children'] = array();
                    makeParentChildRelations($inArray, $tuple['children'], $tuple['id']);
                    $outArray[] = $tuple;   
                }
            }
        }
    
        $outArray = array();
        makeParentChildRelations($inArray, $outArray);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值