RE: [PHP] Trouble with understanding arrays
| From: | Andy Crain | Date: | Thu, 03 Oct 2002 05:47:21 +0000 |
| Subject: | RE: [PHP] Trouble with understanding arrays | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-118696@lists.php.net to get a copy of this message | ||
It looks like your problem is simply in the debug line, where you echo
print_r(array_values($TickersCurrent));
You shouldn't call array_values() before print_r(), since array_values
generates an indexed array of only the values (quotes), not the keys
(tickers), of the supplied array, essentially taking your quotes from
your ticker=>quote pairs and replacing the tickers with an index. Just
do print_r($TickersCurrent) and you'll get the desired result.
Hope that helps,
Andy
-----Original Message-----
From: Christopher J. Crane [mailto:ccrane@inxdesign.com]
Sent: Wednesday, October 02, 2002 8:16 PM
To: php-general@lists.php.net
Subject: [PHP] Trouble with understanding arrays
I am having problems with arrays. I guess I just don't understand them
all
that well.
I have an simple array of stock tickers. Then for each ticker I go to
Yahoo
to get their current price and try to push the Name of the ticker and
it's
value into an associative array(I think). Then I want to sort the array
so
the the values are in order from highest to lowest, so I can see the
highest
amount as the first position and the lowest as the last position.
Here is my problem; for debuggin purposes I do a
print_r(array_values());
and I get the following for output.
Array
(
[0] => 7.28
[1] => 5.20
[2] => 1.969
[3] => 59.63
[4] => 4.43
)
I am not sure why I am getting this. I guess I expected the numerical
positions to be the keys something like:
Array
(
[ikn] => 7.28
[xrx] => 5.20
[danky] => 1.969
[ibm] => 59.63
[rhat] => 4.43
)
Since I am not getting the results I expected I am not sure if the rest
is
working correctly because I do not know how to access the first position
in
the array, or the last. When I get this working, I will be adding 200
tickers and I would like to get the first 5 and the last 5, expecting
them
to be the highest 5 and the lowest 5 respectively. I hope someone will
take
the time to halp me.
Thank you in advance.
Here is the code I am working on:
$Tickers = array("ikn", "xrx", "danky", "ibm",
"rhat");
$TickersCurrent = array();
foreach($Tickers as $Ticker) {
$LookupUrl =
"http://finance.yahoo.com/d/quotes.csv?s=$Ticker&f=l1&e=.txt";
$Current = implode('', file("$LookupUrl"));
$Current = rtrim($Current);
$TickersCurrent["$Ticker"]=$Current;
}
// Done for debuggin only
print "<pre>\n";
print_r(array_values($TickersCurrent));
print "</pre>\n";
asort ($TickersCurrent);
reset ($TickersCurrent);
echo "CTC Indice = " . array_sum($TickersCurrent) . "<br>\n";
echo "The highest Stock is $TickersCurrent[0]";
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php