Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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;}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement