douyuanliao8815 2019-07-25 12:21
浏览 91
已采纳

将XML作为序列化PHP数组保存到数据库中

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

  • 写回答

2条回答 默认 最新

  • draxq02664 2019-07-25 12:32
    关注

    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";}}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,