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