在PHP中将数组转换为字符串

时间:2013-03-14 01:09:59

标签: php arrays

我正在尝试将数组转换为字符串
 从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将带有Attributes的xml转换为数组

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

试试这个:

foreach( $my_array['ChargeableRateInfo']['NightlyRatesPerRoom']['NightlyRate']
         as $k => $rates )
{
    if( array_key_exists( 'baseRate', $rates ) )
    {
        echo $rates['baseRate'], "\n";
    }
}