简体   繁体   English

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

[英]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. 我在对象数组中存储了数千个URL。 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. 我的最终目标是将该关联数组转换为json对象并导出。

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. 将类对象直接转换为json无法正常工作,因此这就是为什么我一直尝试将所有对象属性添加到关联数组的原因。

//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 警告:array_merge():参数2不是C:\\ wamp64 \\ www \\ Scrape v4.0 \\ mainV2.php中的数组

Add a return statement in the merge operation: 在合并操作中添加一个return语句:

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))) : 使用print_r( json_encode( toJson( $testArr))) JSON输出:

[{"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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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