Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML to array

Status
Not open for further replies.

Brianfree

Programmer
Feb 6, 2008
220
GB
Hi, i have the following working code to put xml values into an array.. however sometimes there can be multiple engine codes? how can i write to the array and read back from the array?

Please can anyone advise???

below is my code...

Code:
<?php
$REG = "";
if (isset($_POST["REG"])) {
	$VRM = $_POST["VRM"];
	$xml = file_get_contents("[URL unfurl="true"]http://www.xxx.com/gateway1_0?reg="[/URL] . $REG . ");
}


$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $vals, $index);
xml_parser_free($p);
$output = array();
$desiredOutput = array(''VIN','VEHICLE_YEAR','MAKE','MODEL','ENGINE_NO'); //add the things you want to retrieve here
foreach ($desiredOutput as $o){
    if (isset($index[$o])){
        $key = $index[$o][0];
        $output[$o] = trim( $vals[$key]['value'] );
    }
}
session_register("$VIN");
session_register("$VEHICLE_YEAR");
session_register("$MAKE");
session_register("$MODEL");
session_register("$ENGINE_NO");

Many thanks,

Brian.
 
unless you know for sure whether there is more than one or exactly one each time you will need to use is_array() to determine whether to return a string or an array or whatever.

a well designed data structure from your source would mandate that any field that could have multiple entries was always delivered essentially as an array (or in xml as a sub node of a parent node engine numbers 1->many engine number)

 
Hi jpadie, 90% of the time a vehicle only has 1 engine code but occasionally there is more than one and will need to display it. The xml data i get back simply displays the following for 1 engine number…


<Engine_No>DT 4164 T</Engine_No>

If there is another engine code it is directly below in the xml…


<Engine_No>DT 4164 T</Engine_No>
<Engine_No>AT 3168 T</Engine_No>

If this helps?

Many thanks,

Brian
 
you'll have to manipulate the xml manually then.
 
assuming there is no parent element for those engine numbers, anyway.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top