Convert an array to XML or JSON

后端 未结 4 1089
执念已碎
执念已碎 2020-12-19 22:57

I want to convert the array below

Array
(
    [city] => Array
        (
            [0] => Array
                (
                    [0] => Rd
            


        
相关标签:
4条回答
  • 2020-12-19 23:23

    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.

    0 讨论(0)
  • 2020-12-19 23:29

    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>
    
    0 讨论(0)
  • 2020-12-19 23:31

    JSON, use the json_encode function:

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

    XML, see this question.

    0 讨论(0)
  • 2020-12-19 23:38

    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();
    }
    
    0 讨论(0)
提交回复
热议问题