known reg
known reg

Reputation: 47

Convert an array to XML or JSON

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?

Upvotes: 3

Views: 39613

Answers (4)

Jeffz
Jeffz

Reputation: 2105

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>

Upvotes: 0

Akan Nkweini
Akan Nkweini

Reputation: 64

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();
}

Upvotes: 4

joelkema
joelkema

Reputation: 19

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.

Upvotes: 2

nickb
nickb

Reputation: 59699

JSON, use the json_encode function:

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

XML, see this question.

Upvotes: 4

Related Questions