Home > OS >  SimpleXMLElement expand to repeated values in array
SimpleXMLElement expand to repeated values in array

Time:06-14

Please help me.. I spent a lot of time on it and I think I ran out of ideas.

I have that code for function array_to_xml:

function array_to_xml( $data, &$xml_data ) {
        foreach( $data as $key => $value ) {
            if (!empty($value)) {
                if( is_array($value)) {
                    if (!empty($value["@attributes"])) {
                            $subnode = $xml_data->addChild($key, $value["@value"]);
                            foreach ($value["@attributes"] as $key1 => $val1) {
                                $subnode->addAttribute($key1, $val1);
                            }
                    } else if ($key == "@value") {
                        foreach ($value as $attr => $attrVal) {
                            $subnode = $xml_data->addChild("$attr", $attrVal);
                            array_to_xml($attrVal, $subnode);
                        }
                    } else {
                            if (!empty($value) and $key != "kontakt") {
                                    $subnode = $xml_data->addChild($key);
                                    array_to_xml($value, $subnode);
                                    
                            } else if (!empty($value) and $key == "kontakt") {
                                        $subnode = $xml_data->addChild($key);
                                        array_to_xml($value, $subnode);
                            };  
                    }
                } else {
                        $xml_data->addChild("$key",$value);
                }
            }
        }
    }

When it hits the "contact" attribute, it creates the following structure:

<kontakt>
<1 typ="email">[email protected]</1>
<2 typ="telefon">3123453141</2>
</kontakt>

How should the function be changed so that its result would be the structure shown below?

<kontakt typ="email">[email protected]</kontakt>
<kontakt typ="telefon">3123453141</kontakt>

The array:

"kontakt"=>[ 
1=>[
     '@value' => '[email protected]',
     '@attributes' => ['typ' => 'email'],
    ],
2=>[
     '@value' => '3123453141',
     '@attributes' => ['typ' => 'telefon'],
            ],
          ],

That's the only thing I'm missing for happiness ;)

CodePudding user response:

something like that (i´ve rewritten your function):

<?php

function array_to_xml( $data, &$xml_data ) {
        foreach( $data as $key => $value ) {
          foreach($value as $innerkey => $innervalue) {
            $v = $innervalue['@value'];
            $t = "";
            if(isset($innervalue['@attributes']['typ'])) {
                $t = $innervalue['@attributes']['typ'];  
            }
            $kontakt = $xml_data->addChild('kontakt', $v);
            $kontakt->addAttribute('typ', $t);
          }
        }
    }

$data = array("kontakt"=>[ 
1=>[
     '@value' => '[email protected]',
     '@attributes' => ['typ' => 'email'],
    ],
2=>[
     '@value' => '3123453141',
     '@attributes' => ['typ' => 'telefon'],
            ],
          ]
);
$xml_data = new SimpleXMLElement('<test></test>') ;
array_to_xml($data,$xml_data);
echo $xml_data->asXML();

results in

<?xml version="1.0"?>
<test><kontakt typ="email">[email protected]</kontakt><kontakt typ="telefon">3123453141</kontakt></test>

playground: https://3v4l.org/Qo9uaU#v7.0.33

CodePudding user response:

<?php
//the array
$data = array(
"kontakt"=>[ 
    1=>[
       '@value' => '[email protected]',
       '@attributes' => ['typ' => 'email'],
    ],
    2=>[
     '@value' => '3123453141',
     '@attributes' => ['typ' => 'telefon'],
    ],
 ]);

 //call to function
 $xml = array_to_xml($data);
 show($xml);

//function to convert the array to xml
function array_to_xml( $data) {
$xml = '';
        foreach( $data as $key => $value ) {
            foreach($value as $key1 => $value1){
                $xml .= '<kontakt typ="'.$value1['@attributes']['typ'].'">';
                $xml .= $value1['@value'].'</kontakt>'.PHP_EOL;
            }
        }
        return $xml;
}

 //function to show it in xml form
 function show($data){
 echo '<pre>';
 echo nl2br(str_replace('<', '&lt;', $data));
 echo '</pre>';

 }
?> 

May this will help you. You can configure in case of multiple array. The above function will work in case of the provided string.

  • Related