Tampilkan postingan dengan label XML. Tampilkan semua postingan
Tampilkan postingan dengan label XML. Tampilkan semua postingan

Senin, 19 Desember 2011

How To Create Array to XML with PHP

Tidak ada komentar:
How to Create Array to XML with PHP

Extensible Markup Language (XML) is a markup language which defines a set of rules for encoding documents in a format which is both human-readable and machine-readable. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all  open standards.

<?php
$data = array(
  'name' => array(
    'first' => 'Randy',
    'last' => 'Septian'
  ),
  'phone' => array(
    'personal' => array(
      'mobile' => '085687xxxxx',
      'home' => '0217788xxxx'
    ),
    'office' => array(
      'fax' => '0217788xxxx'
    )
  ),
  'gender' => 'male'
);

$xml = new XmlWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement('root');

function write(XMLWriter $xml, $data){
    foreach($data as $key => $value){
        if(is_array($value)){
            $xml->startElement($key);
            write($xml, $value);
            $xml->endElement();
            continue;
        }
        $xml->writeElement($key, $value);
    }
}
write($xml, $data);
$xml->endElement();
echo $xml->outputMemory(true);
?>