dongzhuxun5136 2019-07-16 19:35
浏览 113

如何获得这个递归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

  • 写回答

1条回答

  • doupu3635 2019-07-16 20:14
    关注

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

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办