How to convert xml string format to php array
18,534
Solution 1
imo, better way is to:
use heredoc with xml definition
$string = <<<XML <?xml version='1.0'?> ... // your xml here XML;
return array, not
SimpleXMLElement Object
if he needs it, with$xml = (array)simplexml_load_string($string);
Solution 2
$str = '<ROOT>
<StartTime>7/9/2013
10:01:29
AM</StartTime>
<EndTime>7/9/2013
10:01:29
AM</EndTime>
<ROW_DATA>
<AMOUNT_ROOMS>2</AMOUNT_ROOMS>
<SUPP_MOVIE_NAME>tiz</SUPP_MOVIE_NAME>
<AMOUNT_NIS>3680</AMOUNT_NIS>
<PRICE_DOCKET_ID>1233</PRICE_DOCKET_ID>
</ROW_DATA>
<ROW_DATA>
<AMOUNT_ROOMS>1</AMOUNT_ROOMS>
<SUPP_MOVIE_NAME>mantiz</SUPP_MOVIE_NAME>
<AMOUNT_NIS>3690</AMOUNT_NIS>
<PRICE_DOCKET_ID>1234</PRICE_DOCKET_ID>
</ROW_DATA>
<StartTime>7/9/2013
10:01:29
AM</StartTime>
<EndTime>7/9/2013
10:01:30
AM</EndTime>
</ROOT>';
$xml = simplexml_load_string($str);
print_r($xml);
Output:
SimpleXMLElement Object
(
[StartTime] => Array
(
[0] => 7/9/2013
10:01:29
AM
[1] => 7/9/2013
10:01:29
AM
)
[EndTime] => Array
(
[0] => 7/9/2013
10:01:29
AM
[1] => 7/9/2013
10:01:30
AM
)
[ROW_DATA] => Array
(
[0] => SimpleXMLElement Object
(
[AMOUNT_ROOMS] => 2
[SUPP_MOVIE_NAME] => tiz
[AMOUNT_NIS] => 3680
[PRICE_DOCKET_ID] => 1233
)
[1] => SimpleXMLElement Object
(
[AMOUNT_ROOMS] => 1
[SUPP_MOVIE_NAME] => mantiz
[AMOUNT_NIS] => 3690
[PRICE_DOCKET_ID] => 1234
)
)
)
Author by
pey22
Updated on June 04, 2022Comments
-
pey22 almost 2 years
Hello i have php var (string) look like xml: its not real xml file just php string with same structure like xml. and i need to get the ROW_DATA fields from it in php loop. (this xml structure i get from soap web service).
7/9/2013 10:01:29 AM 7/9/2013 10:01:29 AM
<ROW_DATA> <AMOUNT_ROOMS>2</AMOUNT_ROOMS> <SUPP_MOVIE_NAME>tiz</SUPP_MOVIE_NAME> <AMOUNT_NIS>3680</AMOUNT_NIS> <PRICE_DOCKET_ID>1233</PRICE_DOCKET_ID> </ROW_DATA> <ROW_DATA> <AMOUNT_ROOMS>1</AMOUNT_ROOMS> <SUPP_MOVIE_NAME>mantiz</SUPP_MOVIE_NAME> <AMOUNT_NIS>3690</AMOUNT_NIS> <PRICE_DOCKET_ID>1234</PRICE_DOCKET_ID> </ROW_DATA> <StartTime>7/9/2013 10:01:29 AM</StartTime> <EndTime>7/9/2013 10:01:30 AM</EndTime>
now i need to get this in php array... any ideas please???
-
brbcoding almost 11 yearsCheck out simplexml
-
vladkras almost 11 yearswhat structure of array do you need?
-
pey22 almost 11 yearsi just need all the ROW_DATA info Something like: ` $room1_AMOUNT = $rooms[0]['AMOUNT_ROOMS']; $room2_AMOUNT = $rooms[1]['AMOUNT_ROOMS']; `
-
-
pey22 almost 11 yearsWOW Thanks for the quick reply. now how can i display them to the browser in loop not in print_r?
-
brbcoding almost 11 years@pey22 throw it in a foreach and iterate over it... You can echo to your page inside the loop or assign variables to be used later.
-
pey22 almost 11 yearsHello vladkras Thanks for the reply . this syntax is ok?
-
vladkras almost 11 years@pey22 hi, yes,
(array)
is official php convertion (scroll down to Converting to array section), also<?xml version='1.0'?>
is not neccessary for simplexml, but I think it's a good coding style