array to xml php issue with the array(s) -


i have created function below:

public function arraytoxml(\simplexmlelement $bodyxml, array $arraytobeconverted)    {        foreach ($arraytobeconverted $element => $value) {            $element = ucfirst($element);            if (is_array($value)) {                $newxmlnode = $bodyxml->addchild($element);                $newxmlnode = $this->arraytoxml($newxmlnode, $value);            } else {                $bodyxml->addchild($element, $value);            }        }        $newxml = $bodyxml;        return $newxml;    }  } 

which converts array xml. trying create duplicates in xml , seem hit issue when use array inside.

the following array...

$testarray = [   "pagination" =>   [     "entriesperpage" => 2,   ],   ["userid" => "usertest1"], ["userid" => "usertest2"], ]; 

outputs section of xml

<pagination> <entriesperpage>2</entriesperpage> </pagination> “<0>" <userid>usertest1</userid> “<1>" <userid>usertest2</userid> 

i don’t want <0> , <1> before userid, understand why these indexes there can’t life of me work out way make work without them. array wizard have ideas please?

thank reading.

try this

public function arraytoxml(\simplexmlelement $bodyxml, array $arraytobeconverted) {    foreach ($arraytobeconverted $element => $value) {        $element = ucfirst($element);        if (is_array($value)) {            $pattern = '/^\d/';             if (!preg_match($pattern, $element)){                $newxmlnode = $bodyxml->addchild($element);                $newxmlnode = $this->arraytoxml($newxmlnode, $value);            }            else{                $bodyxml = $this->arraytoxml($bodyxml, $value);            }         } else {            $bodyxml->addchild($element, $value);        }    }    $newxml = $bodyxml;    return $newxml; } 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -