Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8027691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:53:07+00:00 2026-06-04T23:53:07+00:00

When I do the following $arr[‘exchange’] = array(‘to’ => $to, ‘rate’ => $result[0]); the

  • 0

When I do the following

$arr['exchange'] = array('to' => $to, 'rate' => $result[0]);

the code works however only prints once.

When I do this

$arr['exchange'] .= array('to' => $to, 'rate' => $result[0]);

it prints out

{"from":"NZD","exchange":"ArrayArrayArrayArray"}

What is the correct way to loop though so that it can set 6 sub array in the exchange array?

Here is my full code

<?php


$currencies = array("USD", "NZD", "KWD", "GBP", "AUD");
foreach ($currencies as $from)
{
$arr = array();
$arr['from'] = $from;
//$arr['exchange'] = array();

 foreach ($currencies as $to)
 {
    if($from != $to)
    {

        $url = 'http://finance.yahoo.com/d/quotes.csv?f=l1d1t1&s='.$from.$to.'=X';
        $handle = fopen($url, 'r');

        if ($handle) {
            $result = fgetcsv($handle);
            fclose($handle);
        }
        $results = $result[1].$result[2];
        $arr['exchange'] = array('to' => $to, 'rate' => $result[0]);


    }
 }
 print json_encode($arr);
 print"<br><br>";
}

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T23:53:10+00:00Added an answer on June 4, 2026 at 11:53 pm

    There are a few issues with your code, namely you are looking for the [] notation to append into an array.

    Secondly, I understand what you’re trying to do with that array formation but I’m not really sure why. It seems like it would be easier to create an array like shown below using the keys to keep track of the various exchange rate crosses, this will also be easier to manage on the javascript side of things later.

        $currencies = array("USD", "NZD", "KWD", "GBP", "AUD");
        $cross = array();
        foreach ($currencies as $from) {
            $cross[$from] = array();
            foreach ($currencies as $to) {
                if ($from != $to) {
                    $url = 'http://finance.yahoo.com/d/quotes.csv?f=l1d1t1&s=' . $from . $to . '=X';
                    $handle = fopen($url, 'r');
    
                    if ($handle) {
                        $result = fgetcsv($handle);
                        //echo "$from:$to - <br/>";
                        //var_dump($result);
                        fclose($handle);
                        $cross[$from][$to] = $result[0];
                    }
                } else {
                    $cross[$from][$to] = 1;
                }
            }
            print json_encode($cross);
            print"<br><br>";
        }
    

    This way you’ll end up with something like this:

    {
    "USD": {...},
    "NZD": {"USD":1.532,"NZD":1,"KWD":0.81,"GBP":1.546,"AUD":1.120},
    "KWD": {...},
    "GBP": {...},
    "AUD": {...}
    }
    

    And can access it in javascript like:

    cross[from][to]

    or

    cross.NZD.USD

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following code, why is it necessary for the array (arr) to be
If arr is an array of size 10, in the following code block, how
Given following array: var arr = [undefined, undefined, 2, 5, undefined, undefined]; I'd like
i have the following code int arr[5]; printf(Input values:); for (i=0;i<5;i++) scanf(%d,&arr[i]); pthread_create(&thread1, NULL,
The following CoffeeScript code: for a in arr do_something_with arr.length is compiled to: var
How can I correct the following code? var arr = []; var name =
I have the following piece of code in C++. int arr[] = {1, 2,
Not sure why I'm getting this error. I have the following: int* arr =
In C, when defining an array I can do the following: int arr[] =
Currently, I am creating a 3d array in js using the following: var arr

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.