1

I am trying to convert array into string
array converted from 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()
      )
   )
)

My try was :

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

I used xml2array to convert xml with Attributes into array

4
  • 1
    You don't tell us how you'd like your string to look.... Commented Mar 14, 2013 at 1:11
  • 1
    As you can see from the syntax highlighting there is a syntax error in your loop. Also, what is the question? Commented Mar 14, 2013 at 1:13
  • 1
    First things first. What do you want the end result to look like? Asking for it as a "string" is a bit ambiguous. Commented Mar 14, 2013 at 1:14
  • 1
    My question is how can i loop baseRate because the last code is not working Commented Mar 14, 2013 at 1:17

2 Answers 2

1

Json_encode is the easiest way to convert multidimensional array to string. http://php.net/manual/en/function.json-encode.php

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.