0

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

3
  • 1
    Removing the Number_ means the XML elements have numeric names which are invalid (stackoverflow.com/questions/19677315/…) Commented Jul 25, 2019 at 12:25
  • That's a serialized array with serialize(), not a json array with json_encode() Commented Jul 25, 2019 at 12:36
  • Every time you store serialised data into a database, Codd kills a puppy! Commented Jul 25, 2019 at 12:42

2 Answers 2

2

I'm not a fan of using json_encode() and json_decode() to do this, but it seems a quick enough fudge for what you are after.

As mentioned in the comment, you can't just remove the Number_ off the front of the tag names as numeric tag names in XML are not valid. A quick way round this though is to just convert the data and the use array_values() to remove the key names...

$xml  = simplexml_load_string($data);
$jsond = array_values(json_decode(json_encode($xml), true));
print_r($jsond);

gives...

Array
(
    [0] => Array
        (
            [min_days] => 1
            [max_days] => 3
            [range_cost] => 94
            [cost_applicable] => fixed
        )

    [1] => Array
        (
            [min_days] => 4
            [max_days] => 6
            [range_cost] => 76
            [cost_applicable] => fixed
        )

)

or the output of

print_r(serialize($jsond));

is

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";}}
Sign up to request clarification or add additional context in comments.

Comments

2
    Try this:
    $content = simplexml_load_string($xml); // Parse XML
    $array = json_decode(json_encode($content), true); // Convert to array
    $values = array_values($array); // Return all the values of an array
    $data = serialize($values); // Make serialize
    print_r($data) // Show the output;

    Hope you will get your expected result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.