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 能给我一些人生建议吗
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到
  • ¥15 请教一下,openwrt如何让同一usb储存设备拔插后设备符号不变?
  • ¥30 使用quartz框架进行分布式任务定时调度,启动了两个实例,但是只有一个实例参与调度,另外一个实例没有参与调度,不知道是为什么?请各位帮助看一下原因!!
  • ¥50 怎么获取Ace Editor中的python代码后怎么调用Skulpt执行代码
  • ¥30 fpga基于dds生成幅值相位频率和波形可调的容易信号发生器。
  • ¥15 R语言shiny包和ncdf4包报错
  • ¥15 origin绘制有显著差异的柱状图和聚类热图
  • ¥20 simulink实现滑模控制和pid控制对比,提现前者优势