I have below out using print_r();

how can i display individual like i want to display

[currency] => USD
[rate] => 1.3000

how to use foreach
Thanks

$xml = simplexml_load_file("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
echo $xml->getName() . "<br />";
foreach($xml->children() as $child)
  {
       echo $child->getName()->currency . ": " . $child . "<br />";
       echo '<pre>';
       print_r($child);
       echo '</pre>';
  }

output

SimpleXMLElement Object
(
    [Cube] => SimpleXMLElement Object
        (
            [@attributes] => Array
                (
                    [time] => 2010-07-16
                )

            [Cube] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => USD
                                    [rate] => 1.3000
                                )

                        )

                    [1] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => JPY
                                    [rate] => 112.54
                                )

                        )

                    [2] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => BGN
                                    [rate] => 1.9558
                                )

                        )

                    [3] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => CZK
                                    [rate] => 25.428
                                )

                        )

                    [4] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => DKK
                                    [rate] => 7.4512
                                )

                        )

                    [5] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => EEK
                                    [rate] => 15.6466
                                )

                        )

                    [6] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => GBP
                                    [rate] => 0.84535
                                )

                        )

                    [7] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => HUF
                                    [rate] => 280.85
                                )

                        )

                    [8] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => LTL
                                    [rate] => 3.4528
                                )

                        )

                    [9] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => LVL
                                    [rate] => 0.7091
                                )

                        )

                    [10] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => PLN
                                    [rate] => 4.0914
                                )

                        )

                    [11] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => RON
                                    [rate] => 4.2658
                                )

                        )

                    [12] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => SEK
                                    [rate] => 9.4810
                                )

                        )

                    [13] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => CHF
                                    [rate] => 1.3543
                                )

                        )

                    [14] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => NOK
                                    [rate] => 8.0530
                                )

                        )

                    [15] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => HRK
                                    [rate] => 7.2276
                                )

                        )

                    [16] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => RUB
                                    [rate] => 39.4907
                                )

                        )

                    [17] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => TRY
                                    [rate] => 1.9907
                                )

                        )

                    [18] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => AUD
                                    [rate] => 1.4812
                                )

                        )

                    [19] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => BRL
                                    [rate] => 2.2978
                                )

                        )

                    [20] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => CAD
                                    [rate] => 1.3586
                                )

                        )

                    [21] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => CNY
                                    [rate] => 8.8075
                                )

                        )

                    [22] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => HKD
                                    [rate] => 10.1061
                                )

                        )

                    [23] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => IDR
                                    [rate] => 11757.68
                                )

                        )

                    [24] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => INR
                                    [rate] => 60.8140
                                )

                        )

                    [25] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => KRW
                                    [rate] => 1563.65
                                )

                        )

                    [26] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => MXN
                                    [rate] => 16.6634
                                )

                        )

                    [27] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => MYR
                                    [rate] => 4.1672
                                )

                        )

                    [28] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => NZD
                                    [rate] => 1.8146
                                )

                        )

                    [29] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => PHP
                                    [rate] => 60.177
                                )

                        )

                    [30] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => SGD
                                    [rate] => 1.7893
                                )

                        )

                    [31] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => THB
                                    [rate] => 41.893
                                )

                        )

                    [32] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [currency] => ZAR
                                    [rate] => 9.8064
                                )

                        )

                )

        )

)

Recommended Answers

All 4 Replies

Hi
You can write your code or foreach loop as follows.It works.If you have still query let me know

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
$xml=@simplexml_load_file("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
if($xml)
echo "Files Successfuly loaded <br />";
else
echo "Error <br />";


	foreach($xml->Cube->Cube->Cube as $cube)
	{

		foreach($cube->attributes() as $a => $b) {
    		echo $a,'="',$b,"</br>";
			}
	}
?>

#

foreach($xml->Cube->Cube->Cube as $cube)

Hi
You can write your code or foreach loop as follows.It works.If you have still query let me know

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
$xml=@simplexml_load_file("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
if($xml)
echo "Files Successfuly loaded <br />";
else
echo "Error <br />";


	foreach($xml->Cube->Cube->Cube as $cube)
	{

		foreach($cube->attributes() as $a => $b) {
    		echo $a,'="',$b,"</br>";
			}
	}
?>

#

foreach($xml->Cube->Cube->Cube as $cube)

Hi Dear,
If you have look over xml file which you are using in your query.
its structure is as follow.

<?xml version="1.0" encoding="UTF-8"?>
<gesmes:Envelope>
<gesmes:subject>Reference rates</gesmes:subject>
<gesmes: Sender>
<gesmes:name>European Central Bank</gesmes:name>
</gesmes: Sender>
<Cube>
<Cube time="2010-07-16">
<Cube currency="USD" rate="1.3000"/>
<Cube currency="JPY" rate="112.54"/>
:
:
:
</Cube>
</Cube>
</gesmes:Envelope>


here in xml file root element is gesmes:Envelope and it is arrary which consist array value at index Cube.
xml->Cube

This Cube consist one sub element Cube with attribute time.
xml->Cube->Cube


Above Cube element consist many sub element Cube contains currency and rate as sub element
xml->Cube->Cube->Cube


I think this explanation you can understand more easily.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.