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>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?