I am having difficulty debugging this parsing code for a csv file

I'm playing around with a script to grab a csv file, parse the result and output it.  I'm not real good with PHP. right now the code outputs "Array" as the result of the function in it. Can anyone debug this and help me figure out what it's doing?
<?php
 
$array = array('MSFT','PH','PFE');
 
print getAllQuotes($array);
 
function getAllQuotes($symbolsarray) {
// returns an array with arrays with data
$allQuotes = array();
 
$baseurl = "http://quote.yahoo.com/download/quotes.csv?format=snlx&ext=.csv&symbols=";
// make the symbols urlencoded.
$urlsymbols = urlencode(implode(" ",$symbolsarray));
 
$quotesURL = $baseurl.$urlsymbols;
 
print $quotesURL;
 
$csv = file($quotesURL);
 
foreach ($csv as $line_num => $line) {
// if NOT exists format: "^AEX2","^AEX2", "N/A - <b>0.00</b>","N/A"
// if exists format: "^AEX" ,"AEX-Index","7:37am - <b>326.71</b>", "Amsterdam"
//"AHOc.BA","ROYAL AHOLD","Aug 28, 2002 - <b>16.435</b>","Buenos Aires"
//"MSFT.AS","MICROSOFT CORP","Oct 20 - <b>27.82</b>","Amsterdam"
// Please not the , in the date of AHOc.BA
// shame I don't know a better value for the 'format' in the baseurl. :-(
// split on ","
print $line_num;
 
$parts = explode("\",\"",$line);
$oneQuote = array();
$oneQuote["symbol"] = trim(trim($parts[0]) , "\"");
$oneQuote["name"] = trim(trim($parts[1]) , "\"");
$oneQuote["place"] = trim(trim($parts[3]) , "\"");
 
$val = trim($parts[2] , "\"");
 
// remove all before <b> and remove the last </b>
// remove 6:30am - <b>
$val = substr($val, strpos($val, "<b>")+3) ;
 
// remove </b>
$val = substr($val , 0 , -4) ;
 
$oneQuote["value"] = $val;
 
// add to allQuotes
$allQuotes[$oneQuote["symbol"]] = $oneQuote;
 
}
return $allQuotes;
 
}
 
?>

Open in new window

LVL 1
digitalwavIT Infrastructure ManagerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Sorry - I can't follow all the details of your code, but try running the code snippet and looking at what prints.

Note especially what happens when you print an array vs when you print an element.  Note also the use of var_dump() to inspect the contents of a variable.  Useful in debugging!

HTH, ~Ray
<?php // RAY_print.php
 
echo "<pre>";
 
// AN ARRAY
$my_array = array('MSFT','PH','PFE');
print $my_array;
echo "\n\n"; // SHIM
 
 
 
// AN ITERATION OVER THE ARRAY
foreach ($my_array as $my_element) {
   print "$my_element \n";
}
echo "\n\n"; // SHIM
 
 
 
// A DUMP OF INFORMATION
var_dump($my_array);
echo "\n\n"; // SHIM
 
 
 
// A FORMATTED PRINT
print_r($my_array);
echo "\n\n"; // SHIM
 
 
 
// A FORMATTED PRINT OF THE LAST ELEMENT OF THE ARRAY
print_r($my_element);
 
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
digitalwavIT Infrastructure ManagerAuthor Commented:
That will definitely help me figure it out.  the basic idea is to grab 3 bits of info from the csv file and output them.  Let me see what your debugging can do for me.
0
digitalwavIT Infrastructure ManagerAuthor Commented:
Ok, let me ask this....

It occurred to me that I need to download the csv file to parse it.  Does $csv = file($quotesURL); download it and open it into the $csv variable?
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

digitalwavIT Infrastructure ManagerAuthor Commented:
Got it!  I needed to do a print_r(getAllQuotes($array)); to get the array to output properly.  Thanks!
0
Ray PaseurCommented:
Have a look at this and its output.  I put comment bars with /* **** */ around the areas I modified.

It looks like you are getting the information into a useful format!

HTH, ~Ray
<?php // RAY_digitalwav.php
 
/* ******** */
echo "<pre>";
/* ******** */
 
$array = array('MSFT','PH','PFE');
 
/* ******** */
// print getAllQuotes($array);
print_r (getAllQuotes($array));
/* ******** */
 
 
function getAllQuotes($symbolsarray) {
// returns an array with arrays with data
$allQuotes = array();
 
$baseurl = "http://quote.yahoo.com/download/quotes.csv?format=snlx&ext=.csv&symbols=";
// make the symbols urlencoded.
$urlsymbols = urlencode(implode(" ",$symbolsarray));
 
$quotesURL = $baseurl.$urlsymbols;
 
print $quotesURL;
 
$csv = file($quotesURL);
 
 
/* ******** */
print_r($csv);
/* ******** */
 
foreach ($csv as $line_num => $line) {
// if NOT exists format: "^AEX2","^AEX2", "N/A - <b>0.00</b>","N/A"
// if exists format: "^AEX" ,"AEX-Index","7:37am - <b>326.71</b>", "Amsterdam"
//"AHOc.BA","ROYAL AHOLD","Aug 28, 2002 - <b>16.435</b>","Buenos Aires"
//"MSFT.AS","MICROSOFT CORP","Oct 20 - <b>27.82</b>","Amsterdam"
// Please not the , in the date of AHOc.BA
// shame I don't know a better value for the 'format' in the baseurl. :-(
// split on ","
print $line_num;
 
$parts = explode("\",\"",$line);
$oneQuote = array();
$oneQuote["symbol"] = trim(trim($parts[0]) , "\"");
$oneQuote["name"] = trim(trim($parts[1]) , "\"");
$oneQuote["place"] = trim(trim($parts[3]) , "\"");
 
$val = trim($parts[2] , "\"");
 
// remove all before <b> and remove the last </b>
// remove 6:30am - <b>
$val = substr($val, strpos($val, "<b>")+3) ;
 
// remove </b>
$val = substr($val , 0 , -4) ;
 
$oneQuote["value"] = $val;
 
// add to allQuotes
$allQuotes[$oneQuote["symbol"]] = $oneQuote;
 
}
return $allQuotes;
 
}
 
?>

Open in new window

0
Ray PaseurCommented:
Does $csv = file($quotesURL);

Yes.  See the man page here:
http://us3.php.net/manual/en/function.file.php
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.