dtv8189 2014-04-25 08:37
浏览 16
已采纳

CodeIgniter - 处理简单的XML和PHP。

in the controller I have _send method. This method returns something like below:

 $xmlstr = <<<XML
<?xml version='1.0' standalone='yes' ?>
<status id="555555555" date="Wed, 28 Mar 2013 12:35:00 +0300">
<id>3806712345671174984921381</id>
<id>3806712345671174984921382</id>
<id>3806712345671174984921383</id>
<id>3806712345671174984921384</id>
<state error="Unknown1">Rejected1</state>
<state error="Unknown2">Rejected2</state>
<state error="">Accepted</state>
<state error="">Accepted</state>
</status>
XML;

This method called:

$req = $this->_send('bulk',$all_phones,$this->input->post('message'));

I am unable to create array or object suitable for passing to model for inserting into DB. Below what I have now.

$xml = new SimpleXMLElement($xmlstr);


foreach ($xml as $child) {

                if ($child->getName() == 'id') {
                    $id[] = $child->id;
                }

                if ($child->getName() == 'state') {
                    $state[] = $child;
                    //$state[] = $child['error'];
                }

}

return array_merge($id,$state);

I am attempting to achieve something like this array:

array(0 => array(
                 'id' => '3806712345671174984921381',
                 'state' => 'Rejected1',
                 'state_error' => 'Unknown1'),
      1 => array( ....

Problem with error attribute wich fault array_merge.

Any ideas?

  • 写回答

2条回答 默认 最新

  • dpvhv66448 2014-04-25 08:52
    关注

    This is how you could do it:

    // Load XML
    $xmlstr = '<?xml version="1.0" standalone="yes" ?>
    <status id="555555555" date="Wed, 28 Mar 2013 12:35:00 +0300">
    <id>3806712345671174984921381</id>
    <id>3806712345671174984921382</id>
    <id>3806712345671174984921383</id>
    <id>3806712345671174984921384</id>
    <state error="Unknown1">Rejected1</state>
    <state error="Unknown2">Rejected2</state>
    <state error="">Accepted</state>
    <state error="">Accepted</state>
    </status>';
    $xml = new SimpleXMLElement($xmlstr);
    
    // Init
    $parsed_data = array();
    
    // Parse Id
    foreach ($xml->id as $id)
    {
        $parsed_data[] = array(
            'id' => (string)$id,
            'state' => '',
            'state_error' => ''
        );
    }
    
    // Parse State & State Error
    $i = 0;
    foreach ($xml->state as $state)
    {
        $parsed_data[$i]['state'] = (string)$state;
        $parsed_data[$i]['state_error'] = (string)$state['error'];
        $i++;
    }
    
    // Output
    var_dump($parsed_data);
    

    Here's the output I got:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况