简体   繁体   中英

How can I get this recursive PHP function to create and associative array of my class objects?

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

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM