-1

I am new in php xml. I have below php code

$query = mysql_query("SELECT u.name, u.phone, m.email, m.mobile FROM user u, micards m WHERE m.usr_id=u.id ");

    while ( $row[] = mysql_fetch_assoc($query)) 
}
    foreach(array_filter($row) as $key =$value) {
    $output[$value['phone']]['cards'][] = array(
    'email' =$value['email'],
    'mobile' =$value['mobile'],
    'name' =$value['name']
    );}

Can anyone tell me how can I get xml of this I want xml in below format ?

 <phone>
        <cards>
            <email>..</email>
            <mobile>..</mobile>
            <name>..</name>
        </cards>
        <cards>
            <email>..</email>
            <mobile>..</mobile>
            <name>..</name>
        </cards>    
    </phone> 
    <phone>
    <cards>
            <email>..</email>
            <mobile>..</mobile>
            <name>..</name>
        </cards>
        <cards>
            <email>..</email>
            <mobile>..</mobile>
            <name>..</name>
        </cards>    
    </phone>
Raptor
  • 53,206
  • 45
  • 230
  • 366
  • That is not xml, it is a format that looks a little like xml. – arkascha Apr 29 '14 at 07:52
  • @arkascha Its pseudo xml off course. I guess he wants an actual phone numbef off course inside for example – nl-x Apr 29 '14 at 07:56
  • 2
    Have you even googled array to xml before asking? (-1). Check for example http://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml for a VERY VERY simple short solution – nl-x Apr 29 '14 at 07:57
  • *sidenote:* stop using deprecated `mysql_*` functions. use [MySQLi](http://php.net/manual/en/book.mysqli.php) or [PDO](http://php.net/manual/en/book.pdo.php) instead. Here is a good [tutorial](http://j.mp/PoWehJ) for PDO. – Raptor Apr 29 '14 at 08:48

2 Answers2

0

i hope this will help u

 $xml_student_info = new SimpleXMLElement("<?xml version=\"1.0\"?><phone></phone>");
 $tour_xml_path=set your xml file path;
 array_to_xml1($data_array,$xml_student_info,$tour_xml_path);
function array_to_xml1($student_info, &$xml_student_info,$file_name) {

    foreach($student_info as $key => $value) {
        if(is_array($value)) {
            if(!is_numeric($key)){
                $subnode = $xml_student_info->addChild("$key");
                array_to_xml1($value, $subnode,$file_name);
            }
            else{
                $subnode = $xml_student_info->addChild("item$key");
                array_to_xml1($value, $subnode,$file_name);
            }
        }
        else {
            $xml_student_info->addChild("$key",htmlspecialchars("$value"));
        }
    }
    $xml_student_info->asXML($file_name);   


}
Mayur Kukadiya
  • 994
  • 6
  • 20
0

As per How to convert array to SimpleXML , just do:

$xml = new SimpleXMLElement('<phone/>');
array_walk_recursive($output, array ($xml, 'addChild'));

then get your xml from:

$xml->asXML();
Community
  • 1
  • 1
nl-x
  • 11,762
  • 7
  • 33
  • 61