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

Cœur
38.9k25 gold badges206 silver badges281 bronze badges
asked Mar 14, 2013 at 1:09
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

answered Mar 14, 2013 at 1:18
1

Try this:

foreach( $my_array['ChargeableRateInfo']['NightlyRatesPerRoom']['NightlyRate']
 as $k => $rates )
{
 if( array_key_exists( 'baseRate', $rates ) )
 {
 echo $rates['baseRate'], "\n";
 }
}
answered Mar 14, 2013 at 2:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.