php - how to give array index a named key on runtime -
here trying use xmlserializer class convert data mysql table xml file.i want xml file this
for ,i need change numeric indexes 'book'.currently can generate array of following form:it has numeric indexing ,but purpose both of indexes (0 , 1) have 'book'.
how can change both numeric indexes 'book'??
full code:
include('xmlserializer/xml/serializer.php'); $host='localhost'; $user='root'; $pass=''; $db='xmlserializer'; $dbh=new pdo("mysql:host=$host;dbname=$db",$user,$pass); $sql='select lname,fname employee'; $sth=$dbh->prepare($sql); $sth->execute(); $xml=array('library'=>array()); $i=0; while($result=$sth->fetch(pdo::fetch_assoc)){ array_push($xml['library'],$result); } print_r($xml); $serializer=new xml_serializer(); $result=$serializer->serialize($xml); if($result===true){ file_put_contents('myxml.xml',$serializer->getserializeddata()); }
you need set default tag
in xml_serializer
:
so code should be:
$options = array( "defaulttagname" => "book", ); $serializer = new xml_serializer($options);
check link more information : https://pear.php.net/manual/en/package.xml.xml-serializer.xml-serializer.examples.php
Comments
Post a Comment