1

我正在尝试将数组
转换为从 xml 转换的字符串数组

 [ChargeableRateInfo] => Array(
    [NightlyRatesPerRoom] => Array
    (
      [NightlyRate] => Array
      (
        [0] => Array()
        [1] => Array()
        [0_attr] => Array(
          [promo] => false
          [rate] => 182.46
          [baseRate] => 182.46
        )
        [1_attr] => Array(
          [promo] => false
          [rate] => 182.46
          [baseRate] => 182.46
        )
        [2] => Array()
      )
   )
)

我的尝试是:

foreach ($my_array['ChargeableRateInfo']['NightlyRatesPerRoom'] ['NightlyRate'] as $rates){
   print_r($rates['1_attr']['baseRate']);
   }

我使用 xml2array 将带有属性的 xml 转换为数组

4

2 回答 2

1

Json_encode 是将多维数组转换为字符串的最简单方法。http://php.net/manual/en/function.json-encode.php

于 2013-03-14T01:18:21.350 回答
1

尝试这个:

foreach( $my_array['ChargeableRateInfo']['NightlyRatesPerRoom']['NightlyRate']
         as $k => $rates )
{
    if( array_key_exists( 'baseRate', $rates ) )
    {
        echo $rates['baseRate'], "\n";
    }
}
于 2013-03-14T02:58:00.757 回答