...

View Full Version : Echo(?) multiple level array



abscess
01-06-2006, 01:31 AM
Hi

I am pretty new to PHP and need some help. I have used a script to parse a XML document located at http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml . I have attached an image to show the array produced by this script.

My problem: How can I extract the data from the array? For example, I would like the php to (echo?) to the browser:

CYP 0.5737
USD 1.2088

I want to be able to pick the rates of my choice.
As a newbie to PHP I have tried to read and learn but I cant find any example or text similar enough to my array that I can make my own script.

Velox Letum
01-06-2006, 01:44 AM
If its really an array...


$usd = $array['gesmes:Envelope']['Cube']['Cube']['Cube'][0];
$cyp = $array['gesmes:Envelope']['Cube']['Cube']['Cube'][2];

I think that might get the right values.

abscess
01-06-2006, 01:56 AM
Thank you for the reply! At least I got a value printed this time, though it aint the right one.
I've tried

$usd = $array['gesmes:Envelope']['Cube']['Cube']['Cube'][0];
and $usd = $array['gesmes:Envelope']['Cube']['Cube']['Cube'][1];
echo $usd

both of them returns the value 'Array'

The script used to produce the 'array' looks like this;
<?php
function GetXMLTree ($xmldata)
{
// we want to know if an error occurs
ini_set ('track_errors', '1');

$xmlreaderror = false;

$parser = xml_parser_create ('ISO-8859-1');
xml_parser_set_option ($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option ($parser, XML_OPTION_CASE_FOLDING, 0);
if (!xml_parse_into_struct ($parser, $xmldata, $vals, $index)) {
$xmlreaderror = true;
echo "error";
}
xml_parser_free ($parser);

if (!$xmlreaderror) {
$result = array ();
$i = 0;
if (isset ($vals [$i]['attributes']))
foreach (array_keys ($vals [$i]['attributes']) as $attkey)
$attributes [$attkey] = $vals [$i]['attributes'][$attkey];

$result [$vals [$i]['tag']] = array_merge ($attributes, GetChildren ($vals, $i, 'open'));
}

ini_set ('track_errors', '0');
return $result;
}

function GetChildren ($vals, &$i, $type)
{
if ($type == 'complete') {
if (isset ($vals [$i]['value']))
return ($vals [$i]['value']);
else
return '';
}

$children = array (); // Contains node data

/* Loop through children */
while ($vals [++$i]['type'] != 'close') {
$type = $vals [$i]['type'];
// first check if we already have one and need to create an array
if (isset ($children [$vals [$i]['tag']])) {
if (is_array ($children [$vals [$i]['tag']])) {
$temp = array_keys ($children [$vals [$i]['tag']]);
// there is one of these things already and it is itself an array
if (is_string ($temp [0])) {
$a = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']][0] = $a;
}
} else {
$a = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']][0] = $a;
}

$children [$vals [$i]['tag']][] = GetChildren ($vals, $i, $type);
} else
$children [$vals [$i]['tag']] = GetChildren ($vals, $i, $type);
// I don't think I need attributes but this is how I would do them:
if (isset ($vals [$i]['attributes'])) {
$attributes = array ();
foreach (array_keys ($vals [$i]['attributes']) as $attkey)
$attributes [$attkey] = $vals [$i]['attributes'][$attkey];
// now check: do we already have an array or a value?
if (isset ($children [$vals [$i]['tag']])) {
// case where there is an attribute but no value, a complete with an attribute in other words
if ($children [$vals [$i]['tag']] == '') {
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']] = $attributes;
}
// case where there is an array of identical items with attributes
elseif (is_array ($children [$vals [$i]['tag']])) {
$index = count ($children [$vals [$i]['tag']]) - 1;
// probably also have to check here whether the individual item is also an array or not or what... all a bit messy
if ($children [$vals [$i]['tag']][$index] == '') {
unset ($children [$vals [$i]['tag']][$index]);
$children [$vals [$i]['tag']][$index] = $attributes;
}
$children [$vals [$i]['tag']][$index] = array_merge ($children [$vals [$i]['tag']][$index], $attributes);
} else {
$value = $children [$vals [$i]['tag']];
unset ($children [$vals [$i]['tag']]);
$children [$vals [$i]['tag']]['value'] = $value;
$children [$vals [$i]['tag']] = array_merge ($children [$vals [$i]['tag']], $attributes);
}
} else
$children [$vals [$i]['tag']] = $attributes;
}
}

return $children;
}
$url = "http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"; //URL of the XML FEED

$contents = file_get_contents($url);

$data = GetXMLTree ($contents);
print_r($data);
?>

Further help would be greatly appreciated :)

Velox Letum
01-06-2006, 01:59 AM
Oops! Sorry, I missed another array.


$usd = $array['gesmes:Envelope']['Cube']['Cube']['Cube'][0]['rate'];
$cyp = $array['gesmes:Envelope']['Cube']['Cube']['Cube'][2]['rate'];

Also next time, please be sure to place your code between PHP tags [*php] [*/php] (without the star). It helps us debug, and saves our eyes. :)

abscess
01-06-2006, 02:04 AM
Great! Thank you!! I've tried to make this work for days and nights now and your code made it! :) thank you so much! :):)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum