I have a PHP multidimensional array which looks like this:

Array
(
[EUR] => Array
    (
        [title] => Euro
        [symbol_left] => 
        [symbol_right] =>  €
        [decimal_point] => .
        [thousand_point] => ,
        [decimal_places] => 2
        [currency_value] => 1.32306004
    )

[USD] => Array
    (
        [title] => United States Dollars
        [symbol_left] => $
        [symbol_right] => 
        [decimal_point] => .
        [thousand_point] => ,
        [decimal_places] => 2
        [currency_value] => 1.00000000
    )

)

when I encode it using PHP's json_encode() it looks like this:

{"EUR":{"title":"Euro","symbol_left":"","symbol_right":" €","decimal_point":".","thousand_point":",","decimal_places":"2","currency_value":"1.32306004"},"USD":{"title":"United States Dollars","symbol_left":"$","symbol_right":"","decimal_point":".","thousand_point":",","decimal_places":"2","currency_value":"1.00000000"}}

I have tried some of the standard ways to iterate through this is jQuery such as the following two:

for(item in json_currencies) {
        var cash = json_currencies[item];
        alert(cash.currency_value);
    }

$.each(json_currencies, function(i,obj) {
        alert(obj.currency_value);
    });

The problem with both of these is that they will alert the two 'currency_value' items three times each, instead of just once.

What is the correct way to iterate through this jSON?

share|improve this question
The first example works correctly for me. Didn't try the second. – Michael Berkowski Apr 7 '12 at 3:20
1  
Tried the second and it works too... fiddle – Michael Berkowski Apr 7 '12 at 3:23
yes, they work, however they alert THREE TIMES for each item rather than just once. – Billy Apr 7 '12 at 3:25
Nope, they output only once for me. Chrome & FF both fine. – Michael Berkowski Apr 7 '12 at 3:27
What is "each item"? It alerts once for USD and another one for EUR. Isn't that what you're looking for? – bfavaretto Apr 7 '12 at 3:28
show 1 more comment

closed as too localized by bfavaretto, Jeff Mercado, animuson, trashgod, Martin. Apr 7 '12 at 9:29

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.

1 Answer

If you are using Google Chrome hit F12. Go to console tab.

in your code:

$.each(json_currencies, function(i,obj) {
    //alert(obj.currency_value);
    console.log(obj.currency_value)  //console.log()
});

you should see the output. This is a good way to show the errors/outputs

share|improve this answer
I can see the output perfectly with the alerts. There are no errors. Please read my question better plus my reply above so you understand the problem. – Billy Apr 7 '12 at 3:28
Oops I forgot what I'm about to answer: "use console.log() instead of alerts" - this is the best way to show outputs and errors including objects - this is what I meant. – Peter Wateber Apr 7 '12 at 3:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.