I have a chunk of XML data... a sample looks like this (please note that I am aware that the XML format is a bit odd, but it's nothing something I have control over.)
<COLUMNS> MLSNUM PHOTOCOUNT PHOTODATE LISTDATE EXPIREDATE </COLUMNS>
<DATA> 500493 8 2005-01-12 08:14:24 2005-01-06 00:00:00 2005-07-06 00:00:00 </DATA>
<DATA> 500828 8 2005-01-12 13:43:38 2005-01-10 00:00:00 2005-07-10 00:00:00 </DATA>
<DATA> 500921 8 2005-01-12 15:39:41 2005-01-12 00:00:00 2005-05-12 00:00:00 </DATA>
<DATA> 500899 8 2005-01-12 12:29:50 2005-01-11 00:00:00 2005-04-03 00:00:00 </DATA>
I would like to be able to access it in a script by going
$variable[0][MLSNUM] and have it return 500493
$variable[1][MLSNUM] and have it return 500828
... basically... each line in the first bracket, and each column in the second. Here is the function I created, but I'm having some problems with it. Does anyone have any ideas?
function xml2array2($xml)
{
$xml = htmlspecialchars($xml);
$xml = str_replace(">", ">", $xml);
$xml = str_replace("<", "<", $xml);
$xml = str_replace(""", "\"", $xml);
$s = simplexml_load_string($xml);
// first, get your columns into an array...
$columns = trim($s->COLUMNS);
$columns = explode("\t", $columns);
$c = count($columns);
for ($i=0;$i<$c;$i++)
{
$columnArray[] = "$columns[$i]";
}
// now, for each data element, create an array
$z = "0";
foreach ($s->DATA as $data)
{
$data = trim($s->DATA);
$data = explode("\t", $data);
$c = count($data);
for ($i=0;$i<$c;$i++)
{
foreach($columnArray as $column)
{
$retsXmlArray[$z][$column] = $data[$i];
}
}
$z++;
}
return $retsXmlArray;
}
Thanks!What are the problems you are having with it and are they PHP5-specific?
<COLUMNS> MLSNUM PHOTOCOUNT PHOTODATE LISTDATE EXPIREDATE </COLUMNS>
<DATA> 500493 8 2005-01-12 08:14:24 2005-01-06 00:00:00 2005-07-06 00:00:00 </DATA>
<DATA> 500828 8 2005-01-12 13:43:38 2005-01-10 00:00:00 2005-07-10 00:00:00 </DATA>
<DATA> 500921 8 2005-01-12 15:39:41 2005-01-12 00:00:00 2005-05-12 00:00:00 </DATA>
<DATA> 500899 8 2005-01-12 12:29:50 2005-01-11 00:00:00 2005-04-03 00:00:00 </DATA>
I would like to be able to access it in a script by going
$variable[0][MLSNUM] and have it return 500493
$variable[1][MLSNUM] and have it return 500828
... basically... each line in the first bracket, and each column in the second. Here is the function I created, but I'm having some problems with it. Does anyone have any ideas?
function xml2array2($xml)
{
$xml = htmlspecialchars($xml);
$xml = str_replace(">", ">", $xml);
$xml = str_replace("<", "<", $xml);
$xml = str_replace(""", "\"", $xml);
$s = simplexml_load_string($xml);
// first, get your columns into an array...
$columns = trim($s->COLUMNS);
$columns = explode("\t", $columns);
$c = count($columns);
for ($i=0;$i<$c;$i++)
{
$columnArray[] = "$columns[$i]";
}
// now, for each data element, create an array
$z = "0";
foreach ($s->DATA as $data)
{
$data = trim($s->DATA);
$data = explode("\t", $data);
$c = count($data);
for ($i=0;$i<$c;$i++)
{
foreach($columnArray as $column)
{
$retsXmlArray[$z][$column] = $data[$i];
}
}
$z++;
}
return $retsXmlArray;
}
Thanks!What are the problems you are having with it and are they PHP5-specific?