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
-
1You don't tell us how you'd like your string to look....Wrikken– Wrikken2013年03月14日 01:11:44 +00:00Commented Mar 14, 2013 at 1:11
-
1As you can see from the syntax highlighting there is a syntax error in your loop. Also, what is the question?Sverri M. Olsen– Sverri M. Olsen2013年03月14日 01:13:01 +00:00Commented Mar 14, 2013 at 1:13
-
1First things first. What do you want the end result to look like? Asking for it as a "string" is a bit ambiguous.Nathan Hess– Nathan Hess2013年03月14日 01:14:59 +00:00Commented Mar 14, 2013 at 1:14
-
1My question is how can i loop baseRate because the last code is not workingAhmed Nasr– Ahmed Nasr2013年03月14日 01:17:29 +00:00Commented Mar 14, 2013 at 1:17
2 Answers 2
Json_encode is the easiest way to convert multidimensional array to string. http://php.net/manual/en/function.json-encode.php
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
lang-php