php - Get a dom file content as multidimensional array -


i have xml file named fonts.xml

<fonts>    <font>        <name>abeezee</name>        <category>sans-serif</category>     </font>     <font>        <name>abel</name>        <category>sans-serif</category>     </font> </fonts> 

now want multidimensional array

array = (            0 => array(                         name => azeebee                        category => sans-serif                       ),          1 => array(                        name => abel                       category => sans-serif                      )  ); 

i have tried

$doc = new \domdocument();     $doc->load( '/fonts/font.xml' );     $nodelist = $doc->getelementsbytagname( "font" );     $list = array();     foreach ($nodelist $n)     {         $value = $n->nodevalue;         $list[] = $value;     }      if (count($list) > 0)     {         echo $list[0];     } 

how can multidimension array above xml structure! idea?

$doc = new \domdocument(); $doc->loadxml($str); $nodelist = $doc->getelementsbytagname( "font" ); $list = array(); foreach ($nodelist $n) {    $temp = array();    foreach($n->childnodes $child)      // save children text node       if($child->nodename != '#text') $temp[$child->nodename] = $child->nodevalue;    $list[] = $temp;   } print_r($list); 

demo


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -