3

I want to convert the array below

Array
(
    [city] => Array
        (
            [0] => Array
                (
                    [0] => Rd
                    [1] => E
                )

            [1] => B
            [2] => P
            [3] => R
            [4] => S
            [5] => G
            [6] => C
        )

    [dis] => 1.4
)

into XML format or JSON. Someone may help please?

Doug
  • 6,322
  • 3
  • 29
  • 48
known reg
  • 47
  • 1
  • 2
  • 8
  • possible duplicate of [How to convert array to SimpleXML](http://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml) – nickb Feb 05 '12 at 19:22
  • 1
    Take a look to http://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml for example – Loïc G. Feb 05 '12 at 19:23

4 Answers4

4

This works for associative arrays.

    function array2xml($array, $node_name="root") {
    $dom = new DOMDocument('1.0', 'UTF-8');
    $dom->formatOutput = true;
    $root = $dom->createElement($node_name);
    $dom->appendChild($root);

    $array2xml = function ($node, $array) use ($dom, &$array2xml) {
        foreach($array as $key => $value){
            if ( is_array($value) ) {
                $n = $dom->createElement($key);
                $node->appendChild($n);
                $array2xml($n, $value);
            }else{
                $attr = $dom->createAttribute($key);
                $attr->value = $value;
                $node->appendChild($attr);
            }
        }
    };

    $array2xml($root, $array);

    return $dom->saveXML();
}
4

JSON, use the json_encode function:

<?php echo json_encode( $array); ?>

XML, see this question.

Community
  • 1
  • 1
nickb
  • 59,313
  • 13
  • 108
  • 143
  • json_encode converts the array inton json format but the key has not been removed for the sub array.. can you propose a function to display all the element in json please – known reg Feb 06 '12 at 18:41
2

Which programming language are you using ?

In case you are using PHP you can use the following to convert to JSON:

$json = json_encode($your_array);

And for XML you can check the following answer: How to convert array to SimpleXML.

Hope it helps.

Community
  • 1
  • 1
joelkema
  • 19
  • 3
  • 1
    sorry but it didn't help.. the examples doesn't support numeric index can you give me more feedback on these array please – known reg Feb 06 '12 at 18:40
0

NOTE: numbers for XML element name is not a good idea, so $your_array should not have numbers for keys.

Try this:

$your_array = array(
        'city' => array
            (
            '0' => array('0' => 'Rd', '1' => 'E'),
            '1' => 'B',
            '2' => 'P',
            '3' => 'R',
            '4' => 'S',
            '5' => 'G',
            '6' => 'C'
            ),
        'dis' => '1.4'
        );

Function below calls itself (recursion), so it should work for array of any depth.

Function uses ternary operator:

(condition) ? if true action : if false action

... to check if value called is array.

If it is array, it calls itself (recursion) to dig deeper, if value is not an array, it is being appended to XML object, using array key for element name and array value for element value.

function array_to_xml(array $your_array, SimpleXMLElement $xml){
    foreach ($arr as $k => $v){
        is_array($v) ? array_to_xml($v, $xml->addChild($k)) : $xml->addChild($k, $v);
    }
    return $xml;
}

$your_xml = $this->array_to_xml($your_array, new SimpleXMLElement(''))->asXML();

Now, your array is an XML and is enclosed in $your_xml variable, so you can with it whatever you want.

$your_xml output (e.g. if you 'echo' it) would look like this:

<root>
    <city>
        <0>
            <0>Rd</0>
            <1>E</1>
        </0>
        <1>B</1>
        <2>P</2>
        <3>R</3>
        <4>S</4>
        <5>G</5>
        <6>C</6>
    </city>
    <dis>1.4</dis>
</root>
Jeffz
  • 2,075
  • 5
  • 36
  • 51