How to retrieve info from an array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

How to retrieve info from an array?

Post by lovelf »

I have this big long array:

Code: Select all

Array ( [name] => gesmes:Envelope [attributes] => Array ( [xmlns:gesmes] => http://www.gesmes.org/xml/2002-08-01 [xmlns] => http://www.ecb.int/vocabulary/2002-08-01/eurofxref ) [content] => [children] => Array ( [0] => Array ( [name] => gesmes:subject [attributes] => [content] => Reference rates ) [1] => Array ( [name] => gesmes:Sender [attributes] => [content] => [children] => Array ( [0] => Array ( [name] => gesmes:name [attributes] => [content] => European Central Bank ) ) ) [2] => Array ( [name] => Cube [attributes] => [content] => [children] => Array ( [0] => Array ( [name] => Cube [attributes] => Array ( [time] => 2009-02-23 ) [content] => [children] => Array ( [0] => Array ( [name] => Cube [attributes] => Array ( [currency] => USD [rate] => 1.2798 ) [content] => ) [1] => Array ( [name] => Cube [attributes] => Array ( [currency] => JPY [rate] => 121.09 ) [content] => ) [2] => Array ( [name] => Cube [attributes] => Array ( [currency] => BGN [rate] => 1.9558 ) [content] => ) [3] => Array ( [name] => Cube [attributes] => Array ( [currency] => CZK [rate] => 28.451 ) [content] => ) [4] => Array ( [name] => Cube [attributes] => Array ( [currency] => DKK [rate] => 7.4504 ) [content] => ) [5] => Array ( [name] => Cube [attributes] => Array ( [currency] => EEK [rate] => 15.6466 ) [content] => ) [6] => Array ( [name] => Cube [attributes] => Array ( [currency] => GBP [rate] => 0.87800 ) [content] => ) [7] => Array ( [name] => Cube [attributes] => Array ( [currency] => HUF [rate] => 297.05 ) [content] => ) [8] => Array ( [name] => Cube [attributes] => Array ( [currency] => LTL [rate] => 3.4528 ) [content] => ) [9] => Array ( [name] => Cube [attributes] => Array ( [currency] => LVL [rate] => 0.7070 ) [content] => ) [10] => Array ( [name] => Cube [attributes] => Array ( [currency] => PLN [rate] => 4.6435 ) [content] => ) [11] => Array ( [name] => Cube [attributes] => Array ( [currency] => RON [rate] => 4.2822 ) [content] => ) [12] => Array ( [name] => Cube [attributes] => Array ( [currency] => SEK [rate] => 11.1350 ) [content] => ) [13] => Array ( [name] => Cube [attributes] => Array ( [currency] => CHF [rate] => 1.4908 ) [content] => ) [14] => Array ( [name] => Cube [attributes] => Array ( [currency] => NOK [rate] => 8.7190 ) [content] => ) [15] => Array ( [name] => Cube [attributes] => Array ( [currency] => HRK [rate] => 7.4617 ) [content] => ) [16] => Array ( [name] => Cube [attributes] => Array ( [currency] => RUB [rate] => 46.0205 ) [content] => ) [17] => Array ( [name] => Cube [attributes] => Array ( [currency] => TRY [rate] => 2.1552 ) [content] => ) [18] => Array ( [name] => Cube [attributes] => Array ( [currency] => AUD [rate] => 1.9796 ) [content] => ) [19] => Array ( [name] => Cube [attributes] => Array ( [currency] => BRL [rate] => 3.0459 ) [content] => ) [20] => Array ( [name] => Cube [attributes] => Array ( [currency] => CAD [rate] => 1.6001 ) [content] => ) [21] => Array ( [name] => Cube [attributes] => Array ( [currency] => CNY [rate] => 8.7487 ) [content] => ) [22] => Array ( [name] => Cube [attributes] => Array ( [currency] => HKD [rate] => 9.9226 ) [content] => ) [23] => Array ( [name] => Cube [attributes] => Array ( [currency] => IDR [rate] => 15255.22 ) [content] => ) [24] => Array ( [name] => Cube [attributes] => Array ( [currency] => INR [rate] => 63.7660 ) [content] => ) [25] => Array ( [name] => Cube [attributes] => Array ( [currency] => KRW [rate] => 1910.81 ) [content] => ) [26] => Array ( [name] => Cube [attributes] => Array ( [currency] => MXN [rate] => 18.8611 ) [content] => ) [27] => Array ( [name] => Cube [attributes] => Array ( [currency] => MYR [rate] => 4.6783 ) [content] => ) [28] => Array ( [name] => Cube [attributes] => Array ( [currency] => NZD [rate] => 2.4967 ) [content] => ) [29] => Array ( [name] => Cube [attributes] => Array ( [currency] => PHP [rate] => 61.370 ) [content] => ) [30] => Array ( [name] => Cube [attributes] => Array ( [currency] => SGD [rate] => 1.9537 ) [content] => ) [31] => Array ( [name] => Cube [attributes] => Array ( [currency] => THB [rate] => 45.593 ) [content] => ) [32] => Array ( [name] => Cube [attributes] => Array ( [currency] => ZAR [rate] => 12.7708 ) [content] => ) ) ) ) ) ) )
Which can be preferably read like this:

Code: Select all

Array
(
    [name] => gesmes:Envelope
    [attributes] => Array
        (
            [xmlns:gesmes] => http://www.gesmes.org/xml/2002-08-01
            [xmlns] => http://www.ecb.int/vocabulary/2002-08-01/eurofxref
        )
 
    [content] => 
    [children] => Array
        (
            [0] => Array
                (
                    [name] => gesmes:subject
                    [attributes] => 
                    [content] => Reference rates
                )
 
            [1] => Array
                (
                    [name] => gesmes:Sender
                    [attributes] => 
                    [content] => 
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [name] => gesmes:name
                                    [attributes] => 
                                    [content] => European Central Bank
                                )
 
                        )
 
                )
 
            [2] => Array
                (
                    [name] => Cube
                    [attributes] => 
                    [content] => 
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [name] => Cube
                                    [attributes] => Array
                                        (
                                            [time] => 2009-02-23
                                        )
 
                                    [content] => 
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => USD
                                                            [rate] => 1.2798
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [1] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => JPY
                                                            [rate] => 121.09
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [2] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => BGN
                                                            [rate] => 1.9558
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [3] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => CZK
                                                            [rate] => 28.451
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [4] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => DKK
                                                            [rate] => 7.4504
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [5] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => EEK
                                                            [rate] => 15.6466
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [6] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => GBP
                                                            [rate] => 0.87800
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [7] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => HUF
                                                            [rate] => 297.05
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [8] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => LTL
                                                            [rate] => 3.4528
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [9] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => LVL
                                                            [rate] => 0.7070
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [10] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => PLN
                                                            [rate] => 4.6435
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [11] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => RON
                                                            [rate] => 4.2822
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [12] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => SEK
                                                            [rate] => 11.1350
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [13] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => CHF
                                                            [rate] => 1.4908
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [14] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => NOK
                                                            [rate] => 8.7190
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [15] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => HRK
                                                            [rate] => 7.4617
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [16] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => RUB
                                                            [rate] => 46.0205
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [17] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => TRY
                                                            [rate] => 2.1552
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [18] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => AUD
                                                            [rate] => 1.9796
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [19] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => BRL
                                                            [rate] => 3.0459
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [20] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => CAD
                                                            [rate] => 1.6001
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [21] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => CNY
                                                            [rate] => 8.7487
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [22] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => HKD
                                                            [rate] => 9.9226
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [23] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => IDR
                                                            [rate] => 15255.22
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [24] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => INR
                                                            [rate] => 63.7660
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [25] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => KRW
                                                            [rate] => 1910.81
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [26] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => MXN
                                                            [rate] => 18.8611
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [27] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => MYR
                                                            [rate] => 4.6783
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [28] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => NZD
                                                            [rate] => 2.4967
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [29] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => PHP
                                                            [rate] => 61.370
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [30] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => SGD
                                                            [rate] => 1.9537
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [31] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => THB
                                                            [rate] => 45.593
                                                        )
 
                                                    [content] => 
                                                )
 
                                            [32] => Array
                                                (
                                                    [name] => Cube
                                                    [attributes] => Array
                                                        (
                                                            [currency] => ZAR
                                                            [rate] => 12.7708
                                                        )
 
                                                    [content] => 
                                                )
 
                                        )
 
                                )
 
                        )
 
                )
 
        )
 
)
How could I get USD rate, CAD rate, JPY rate, or anything from there?

I would like to set in variables a few rates from this array but I do not know how to do it.

Like, what is the route to a rate value for example? I am struggling to find the route.

This is the function:

Code: Select all

<?php
function xml_to_array( $file )
{
    $parser = xml_parser_create();
    xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, 0 );
    xml_parser_set_option( $parser, XML_OPTION_SKIP_WHITE, 1 );
    xml_parse_into_struct( $parser, file_get_contents($file), $tags );
    xml_parser_free( $parser );
    
    $elements = array();
    $stack = array();
    foreach ( $tags as $tag )
    {
        $index = count( $elements );
        if ( $tag['type'] == "complete" || $tag['type'] == "open" )
        {
            $elements[$index] = array();
            $elements[$index]['name'] = $tag['tag'];
            $elements[$index]['attributes'] = $tag['attributes'];
            $elements[$index]['content'] = $tag['value'];
            
            if ( $tag['type'] == "open" )
            {    # push
                $elements[$index]['children'] = array();
                $stack[count($stack)] = &$elements;
                $elements = &$elements[$index]['children'];
            }
        }
        
        if ( $tag['type'] == "close" )
        {    # pop
            $elements = &$stack[count($stack) - 1];
            unset($stack[count($stack) - 1]);
        }
    }
    return $elements[0];
 
}
$lero = xml_to_array('http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml');
 
   echo '<pre>';
   print_r($lero);
   echo '</pre>';
?>
I want to add to this last piece of code something like this:

Code: Select all

$ratea = thevalueinarrayaboveforusd;
$rateb = thevalueinarrayaboveforjpy;
$ratec = thevalueinarrayaboveforgbp;
$rated = thevalueinarrayaboveforcad;
$ratee = thevalueinarrayaboveforeur;
$ratef = thevalueinarrayaboveforaud;
 
With what I already did I just need to know the "route" to those values in the array, right?
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: How to retrieve info from an array?

Post by semlar »

Are these the values you want?

Code: Select all

 
foreach($lero['children'][2]['children'][0]['children'] as $cube) {
    echo "<li>{$cube['attributes']['currency']} => {$cube['attributes']['rate']}</li>";
}
 
I'll explain how to navigate arrays if you can't figure it out from this example.

(Start with the key you want and work backwards)

Code: Select all

 
[color=#FF0000][children] => Array[/color]
    (
        [0] => Array
            (
                [name] => gesmes:subject
                [attributes] => 
                [content] => Reference rates
            )
 
        [1] => Array
            (
                [name] => gesmes:Sender
                [attributes] => 
                [content] => 
                [children] => Array
                    (
                        [0] => Array
                            (
                                [name] => gesmes:name
                                [attributes] => 
                                [content] => European Central Bank
                            )
 
                    )
 
            )
 
        [color=#FF0000][2] => Array[/color]
            (
                [name] => Cube
                [attributes] => 
                [content] => 
                [color=#FF0000][children] => Array[/color]
                    (
                        [color=#FF0000][0] => Array[/color]
                            (
                                [name] => Cube
                                [attributes] => Array
                                    (
                                        [time] => 2009-02-23
                                    )
 
                                [content] => 
                                [color=#FF0000][children] => Array[/color]
                                    (
                                        [color=#FF0000][0] => Array[/color]
                                            (
                                                [name] => Cube
                                                [color=#FF0000][attributes] => Array[/color]
                                                    (
                                                        [color=#FF0000][currency] => USD[/color]
                                                        [color=#FF0000][rate] => 1.2798[/color]
                                                    )
 
                                                [content] => 
                                            )
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: How to retrieve info from an array?

Post by lovelf »

Thank you very much semlar!

I struggled all night trying to find out this, how could I store the outcome in separate variables?

Thank you! :P
semlar
Forum Commoner
Posts: 61
Joined: Fri Feb 20, 2009 10:45 pm

Re: How to retrieve info from an array?

Post by semlar »

Personally I would just stick them into an array and call them like $rate['USD']. You could write it to store each one in an isolated variable, but it seems like it would be more trouble than it's worth.

For example with an array..

Code: Select all

 
foreach($lero['children'][2]['children'][0]['children'] as $cube) {
    $rate[$cube['attributes']['currency']] = $cube['attributes']['rate'];
}
 
echo "USD rate: {$rate['USD']}";
 
echo '<pre>';
print_r($rate);
echo '</pre>';
 
Or with variable variables..

Code: Select all

 
foreach($lero['children'][2]['children'][0]['children'] as $cube) {
    $$cube['attributes']['currency'] = $cube['attributes']['rate'];
}
 
echo "USD rate: {$USD}";
 
Or with regular? variables..

Code: Select all

 
foreach($lero['children'][2]['children'][0]['children'] as $cube) {
    if($cube['attributes']['currency'] == "USD") $ratea = $cube['attributes']['rate'];
    if($cube['attributes']['currency'] == "JPY") $rateb = $cube['attributes']['rate'];
    if($cube['attributes']['currency'] == "GBP") $ratec = $cube['attributes']['rate'];
    //...
}
 
echo "USD rate: {$ratea}";
 
Well, you get the idea.
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: How to retrieve info from an array?

Post by lovelf »

OK, thank you again semlar. :drunk:
Post Reply