PHP Array to XML dilemma PHP Array to XML dilemma json json

PHP Array to XML dilemma


Lucky you, I was just writing this kind of thing for myself... Basically you supply a list of elements you want to use, by default it will use they key/index. hope this helps.

<?PHPclass Serializer{       private static function getTabs($tabcount)    {        $tabs = '';        for($i = 0; $i < $tabcount; $i++)        {            $tabs .= "\t";        }        return $tabs;    }    private static function asxml($arr, $elements = Array(), $tabcount = 0)    {        $result = '';        $tabs = self::getTabs($tabcount);        foreach($arr as $key => $val)        {            $element = isset($elements[0]) ? $elements[0] : $key;            $result .= $tabs;            $result .= "<" . $element . ">";            if(!is_array($val))                $result .= $val;            else            {                $result .= "\r\n";                $result .= self::asxml($val, array_slice($elements, 1, true), $tabcount+1);                $result .= $tabs;            }            $result .= "</" . $element . ">\r\n";        }        return $result;    }    public static function toxml($arr, $root = "xml", $elements = Array())    {        $result = '';        $result .= "<" . $root . ">\r\n";        $result .= self::asxml($arr, $elements, 1);         $result .= "</" . $root . ">\r\n";        return $result;    }}    $arr = Array (    0 => Array    (        'Key1' => 'value1',        'Key2' => 'value2',        'Key3' => 'value3',        'Key4' => 'value4',    ),    1 => Array    (        'Key1' => 'value1',        'Key2' => 'value2',        'Key3' => 'value3',        'Key4' => 'value4',    ),    2 => Array    (        'Key1' => 'value1',        'Key2' => 'value2',        'Key3' => 'value3',        'Key4' => 'value4',    ),);?>

Example 1

echo Serializer::toxml($arr, "xml", array("result"));    //output<xml>    <result>        <Key1>value1</Key1>        <Key2>value2</Key2>        <Key3>value3</Key3>        <Key4>value4</Key4>    </result>    <result>        <Key1>value1</Key1>        <Key2>value2</Key2>        <Key3>value3</Key3>        <Key4>value4</Key4>    </result>    <result>        <Key1>value1</Key1>        <Key2>value2</Key2>        <Key3>value3</Key3>        <Key4>value4</Key4>    </result></xml>

Exmaple 2

echo Serializer::toxml($arr, "xml", array("result", "item"));// output<xml>    <result>        <item>value1</item>        <item>value2</item>        <item>value3</item>        <item>value4</item>    </result>    <result>        <item>value1</item>        <item>value2</item>        <item>value3</item>        <item>value4</item>    </result>    <result>        <item>value1</item>        <item>value2</item>        <item>value3</item>        <item>value4</item>    </result></xml>

Example 3

echo Serializer::toxml($arr, "xml", array(null, "item"));// output<xml>    <0>        <item>value1</item>        <item>value2</item>        <item>value3</item>        <item>value4</item>    </0>    <1>        <item>value1</item>        <item>value2</item>        <item>value3</item>        <item>value4</item>    </1>    <2>        <item>value1</item>        <item>value2</item>        <item>value3</item>        <item>value4</item>    </2></xml>


I don't think you would need to use a library for this.

echo("<xml>");foreach($data as $k => $v) {    echo("<result>");    foreach($v as $i => $j)        echo("<".$i.">" . $j . "</".$i.">");    echo("</result>");}echo("</xml>");


Assuming your outer array is called $array use the [] syntax. This will give you an array key called 'result' which itself is an indexed array. When converted to XML, I believe (though have not tested) that its output will be what you are looking for.

$results = array('result' => array());// Looping over the outer array gives you the inner array of keysforeach ($array as $result) {    // Append the array of keys to the results array    $results['result'][] = $result;}print_r($results);echo json_encode($results);

Now use your array to XML library to make the XML.