如何获得这个递归PHP函数来创建我的类对象的关联数组?

I have thousands of urls stored in an array of objects. I want to take the hierarchy of classes that I have built and put it in the form of an associative array. However, When I write the recursive function I am having trouble wrapping my brain around how to get it to work the way I want it to. My end goal is to convert this associative array to a json object and export it.

Converting my class objects straight to json is not working, so this is why I have been trying to add all of the object attributes to an associative array.

//ChildNode class

class ChildNode extends PNode
{
    public $parent;

    public function __construct($url, PNode $parent)
    {
        parent::__construct($url);
        $this->parent = $parent;

    }


    public function getParent()
    {
        return $this->parent;
    }


    public function setParent($parent)
    {
        $this->parent = $parent;
    }


}

//PNode Class

class PNode
{
    public $url;
    public $dir;
    public $children;
    public $title;

    public function __construct($url)
    {
        $this->url = $url;
        $this->children = array();
        $this->dir = parse_url($url, PHP_URL_PATH);
        $html = file_get_html($url);
        $raw = $html->find('title',0);
        $this->title = $raw->innertext;
    }


    public function getUrl()
    {
        return $this->url;
    }


    public function setUrl($url)
    {
        $this->url = $url;
    }

    public function getChildren()
    {
        return $this->children;
    }

    public function setChildren($children)
    {
        $this->children = $children;
    }
    public function addChild(ChildNode $childNode){
        $this->children[] = $childNode;

    }


    public function getDir(){

        return $this->dir;
    }

    public function getTitle(){
        return $this->title;
    }

    public function getParent(){
        return $this;
    }







}

//main .php file

//$testArr is an array of PNodes each PNode has an array of ChildNodes
//and a ChildNode can also have an Array of ChildNodes

var_dump(toJson($testArr[0]->getChildren()));

function toJson($arr){
    $temp = array();

    if($arr!=null){

        foreach ($arr as $item){

            $temp[] = ["url"=>$item->getUrl(),"Title"=>$item->getTitle(), "children"=>$item->getChildren()];

            $temp = array_merge($temp, toJson($item->getChildren()));


        }


    }
    else{return $temp;}




}

I get this warning and am not sure what to do about it. I cannot figure out how to pass the temporary array to the function while simultaneously adding that to itself and returning the final result.

Warning: array_merge(): Argument #2 is not an array in C:\wamp64\www\Scrape v4.0\mainV2.php

doushoubu5360
doushoubu5360 $a总是一个数组?因为你的错误建议不然......
一年多之前 回复
duandui2803
duandui2803 是的,遗憾的是我仍然收到同样的错误。
一年多之前 回复
doutuanxiao4619
doutuanxiao4619 你试过$a=toJson($item->getChildren());合并之前和var_dump$a?
一年多之前 回复

1个回答

Add a return statement in the merge operation:

return array_merge( $temp, toJson( $item->getChildren()));

Do not append children to the temp array since you are going to add children recursively anyway. Instead, just add the child count.

JSON output using print_r( json_encode( toJson( $testArr))):

[{"url":"http:\/\/abc","Title":null,"ChildCount":1},{"url":"http:\/\/abc\/a1","Title":null,"ChildCount":0}]

Here is the modified function:

function toJson( $arr ) {
    $temp = array();
    if ( $arr != null ) {
        foreach ( $arr as $item ) {
            $temp[] = [ "url" => $item->getUrl(), "Title" => $item->getTitle(), "ChildCount" => sizeof($item->getChildren())];
            return array_merge( $temp, toJson( $item->getChildren() ) );
        }
    }
    return $temp;
}
douruanfan3030
douruanfan3030 好的,现在我到了某个地方。 但是,输出不是预期的,并且它不允许我使用json_encode()将输出转换为json。 我的类层次结构就像一个网页。 每个代表一个网页和该网页上显示的所有其他网址。 我的json对象需要类似。 从所有“顶级”网页和他们的孩子开始,作为许多长度的各个分支。
一年多之前 回复
douqilin4296
douqilin4296 但是返回将离开函数而不添加所有子项 - 不要认为这是OP想要它的方式。 回报应该在最后
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐