dsn5510 2016-01-30 00:22
浏览 45
已采纳

如何动态显示相应的输入数组的xml标签?

My code is like this : http://pastebin.com/G74JY27f

<?php
$param = array('sex'=>'Mr.',
  'family_name'=>'Mourinho',
  'first_name'=>'Jose',
  'booking_phone'=>'123456',
  'booking_mobile'=>'123',
  'booking_email'=>'mourinho@gmail.com',
  'passenger_sex'=>array(
    'Sgl'=>array('Mr.'),
    'Dbl'=>array('Mr.','Mr.','Mr.','Mr.'),
  ),
  'passenger_family_name'=>array(
    'Sgl'=>array('Bale'),
    'Dbl'=>array('Hazard','Ronaldo','Messi','Beckham'),
  ),
  'passenger_first_name'=>array(
    'Sgl'=>array('Gareth'),
    'Dbl'=>array('Eden','Christiano','Lionel','David'),
  )
);

echo '<pre>';print_r($param);echo '</pre>';

$xml = "<?xml version='1.0' encoding='utf-8' ?>

  <Request>

    <BookingRequest>                            

      <Name>".$param['first_name']."</Name>

      <Email>".$param['booking_email']."</Email>

      <Phone>".$param['booking_phone']."</Phone>

      <MPhone>".$param['booking_mobile']."</MPhone>

      <Passenger Room='SGL'>

        <PassSex>Mr</PassSex>

        <PassLocalNm>Gareth</PassLocalNm>

        <PassFirstNm> Gareth </PassFirstNm>

        <PassLastNm> Bale</PassLastNm>

      </Passenger>  

    </BookingRequest>

  </Request>";

$json = json_encode($xml);
$response_array = json_decode($json,TRUE);
print_r($response_array);
die();
?>

The result of print_r($param), it is an array

The result of print_r($response_array), it is xml

Name tag, email tag, phone tag and mphone tag in xml, that according to the data from the array

But the problem is the passenger tag, it still static

Seems using foreach, but I am still confused

The result there will be five passenger tag

So I want output like this http://pastebin.com/t380Aits

How to order the output looks like the above link?

  • 写回答

1条回答 默认 最新

  • douou0977 2016-01-30 06:02
    关注

    You can use foreach loops to add the passenger tags to the xml variable.

    You would do so by looping through one of the multidimensional parameters, and then looping through the values and using the keys of each array to get the related data from the other arrays.

    See the below working code for an example of how to do this.

    $xml = "<?xml version='1.0' encoding='utf-8' ?>
    
        <Request>
    
            <BookingRequest>                              
    
                <Name>".$param['first_name']."</Name> 
    
                <Email>".$param['booking_email']."</Email> 
    
                <Phone>".$param['booking_phone']."</Phone> 
    
                <MPhone>".$param['booking_mobile']."</MPhone>
    ";
    // loop through one of the multi dimensional arrays
    foreach ($param['passenger_sex'] as $type => $values)
    {
        // loop through all values and add a Passenger record to $xml
        foreach ($values as $key => $val)
        {
            // use the $type array key, and the $key from the second loop to get the values
            $xml .= "
                <Passenger Room='".$type."'>
    
                    <PassSex>".$param['passenger_sex'][$type][$key]."</PassSex>
    
                    <PassLocalNm>".$param['passenger_first_name'][$type][$key]."</PassLocalNm>
    
                    <PassFirstNm> ".$param['passenger_first_name'][$type][$key]." </PassFirstNm>
    
                    <PassLastNm> ".$param['passenger_family_name'][$type][$key]."</PassLastNm>
    
                </Passenger>
    ";
        }
    }
    // add the closing tags to the xml string
    $xml .= "
            </BookingRequest>
    
        </Request>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题