php multidimensional array recursively into an object -


i have multidimensional array create object can output both xml , json.

i having difficulty getting head around how recursively. have looked @ many multidimensional posts can find on here still stuck.

what doing wrong?

class dataset {     public $name;     public $attr = array();     public $children = array();      function __construct($name){         $this->name = $name;     }      function addattr($attr){         $this->attr[] = $attr;     }      function addchildren($children){         $this->children[] = $children;     } }    $jazzy = array(     name => 'aaa',     attr => array(         id => 123     ),     children => array(         array(             name => 'name',             attr => array(),             children => array(                 'www'             ),         ),         array(             name => 'websites',             attr => array(),             children => array(                 array(                     name => 'website',                     attr => array(                         id => 456,                         class => 'boom'                      ),                     children => array(                         array(                             name => 'url',                             attr => array(),                             children => array(                                 'www.test.com'                             )                         )                     )                 ),                 array(                     name => 'website',                     attr => array(                         id => 123,                         class => "boom"                     ),                     children => array(                         array(                             name => 'url',                             attr => array(),                             children => array(                                 'www.example.com'                             )                         )                     )                 )             )         )     ) ); 

i looking create output

<aaa id="123">     <name>www</name>     <websites>         <website id='456' class="boom">             <url>www.test.com</url>         </website>         <website id='123 class="boom">             <url>www.example.com</url>         </website>     </websites> </aaa> 

my code

function arraytodataset($array, $node){      foreach ($array $key => $value) {         $name = $array['name'];         $attr = $array['attr'];         $children = $array['children'];          if($key == "name"){             $name = $value;             $node->addname($name);         }         elseif($key == "attr"){             $attr = $value;          $node->addattr($attr);         }          elseif($key == "children")         {             $children = $value;             $newnode = new dataset();              foreach($children $k => $v)             {                 $newnode = $node->addchildren($v);             }             return arraytodataset($children, $newnode);         }     } }  $node = new dataset(); $thing = arraytodataset($jazzy, $node); print_r($thing); 

this may way parse data dataset object, use output in other format xml or json. there may easier ways though...

class dataset {     public $name;     public $attr = array();     public $children = array();     public $url = array();      function __construct($name)     {         $this->name = $name;     }      function addattr($attr, $value)     {         $this->attr[$attr] = $value;     }      function addchild(dataset $child)     {         $this->children[] = $child;     }      function addurl($url) {         $this->url[] = $url;     }      static function makenode($array)     {         // $array needs have required elements         $node = new dataset($array['name']);         if (isset($array['attr'])) {             foreach ($array['attr'] $k => $v) {                 if (is_scalar($v)) {                     $node->addattr($k, $v);                 }             }         }         if (isset($array['children']) && is_array($array['children'])) {             foreach ($array['children'] $c) {                 if(is_scalar($c)) {                     $node->addurl($c);                 } else {                     $node->addchild(self::makenode($c));                 }             }         }         return $node;     }  }  print_r(dataset::makenode($jazzy)); 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -