I'd like to serialize a XML array with a custom function, but somehow all approaches I tried were not successful
So far I played around with string manipulations and json_encode without success. Since there are numerical elements, I added a "Number_" to the XML as you see in . Now for creating the array I first remove those by str_replace and then load the XML, encode, decode and return.
This is the XML i try to save:
<my_cost>
<Number_1>
<min_days>1</min_days>
<max_days>3</max_days>
<range_cost>94</range_cost>
<cost_applicable>fixed</cost_applicable>
</Number_1>
<Number_2>
<min_days>4</min_days>
<max_days>6</max_days>
<range_cost>76</range_cost>
<cost_applicable>fixed</cost_applicable>
</Number_2>
</my_cost>
this is the code so far:
$data = str_replace("Number_", "", $data);
$xml = simplexml_load_string($data);
$json = json_encode($xml);
$jsond = json_decode($json, true);
return $jsond;
The expected PHP array should look as follows:
a:2:{
i:0;a:4{s:8:"min_days";s:1:"1";s:8:"max_days";s:1:"3";s:10:"range_cost";s:2:"94";s:15:"cost_applicable";s:5:"fixed";}
i:1;a:4:{s:8:"min_days";s:1:"4";s:8:"max_days";s:1:"6";s:10:"range_cost";s:2:"76";s:15:"cost_applicable";s:5:"fixed";}}
Actual result is empty