denoteone Posted March 19, 2009 Share Posted March 19, 2009 I think I have an issue with my array but and not sure. can anyone see what I am doing wrong? $symbol_list = array("MCD", "^DJI"); $feed = GetQuote($symbol_list); $sym_list = split(", ", $symbol); foreach ($sym_list as $value) { echo $value; } Am I using this is the correct way? Quote Link to comment https://forums.phpfreaks.com/topic/150181-solved-foreach/ Share on other sites More sharing options...
Ayon Posted March 19, 2009 Share Posted March 19, 2009 when you use the array() the comman creates an array starting at 0 array("MCD", "^DJI"); returns array( [0] MCD [1] ^DJI ); so you don't need the explode(); just remove the explode() line and do this: foreach ($symbol_list as $value) { echo $value; } Quote Link to comment https://forums.phpfreaks.com/topic/150181-solved-foreach/#findComment-788681 Share on other sites More sharing options...
Maq Posted March 19, 2009 Share Posted March 19, 2009 What is in $symbol and where does it come from? note: if you're not using a regex for the split, use explode(), it's faster. If you do use a regex use preg_split() $sym_list = explode(", ", $symbol); Quote Link to comment https://forums.phpfreaks.com/topic/150181-solved-foreach/#findComment-788684 Share on other sites More sharing options...
denoteone Posted March 19, 2009 Author Share Posted March 19, 2009 here is the code in full. the symbol is the symbol is the symbol_list array. $symbol_list = array("MCD", "^DJI"); $feed = GetQuote($symbol_list); echo $feed; $fp = fopen("drop.xml", "a"); fwrite($fp, $feed); function GetQuote($symbol) { // Build xml result as we go $xml_result = "<StockQuotes>"; // create symbol list $sym_list = split(", ", $symbol); foreach ($sym_list as $value) { echo $value; } Quote Link to comment https://forums.phpfreaks.com/topic/150181-solved-foreach/#findComment-788689 Share on other sites More sharing options...
Maq Posted March 19, 2009 Share Posted March 19, 2009 Try: $symbol_list = array("MCD", "^DJI"); $feed = GetQuote($symbol_list); echo $feed; $fp = fopen("drop.xml", "a"); fwrite($fp, $feed); fclose($fp); function GetQuote($symbol) { // Build xml result as we go $xml_result = "\n"; // Grab each stock symbol and concatenate it to $xml_result foreach ($symbol as $value) { $xml_result .= $value . "\n"; } $xml_result .= ""; return $xml_result; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/150181-solved-foreach/#findComment-788701 Share on other sites More sharing options...
Mark Baker Posted March 19, 2009 Share Posted March 19, 2009 Why are you executing split() against $symbol_list when it's already an array either $symbol_list = "MCD,^DJI"; or comment out // $sym_list = split(", ", $symbol); and change foreach ($sym_list as $value) to foreach ($symbol as $value) Quote Link to comment https://forums.phpfreaks.com/topic/150181-solved-foreach/#findComment-788704 Share on other sites More sharing options...
denoteone Posted March 19, 2009 Author Share Posted March 19, 2009 on fopen what mode will open the file for writing and overwrite all data in the file with the new data? Quote Link to comment https://forums.phpfreaks.com/topic/150181-solved-foreach/#findComment-788713 Share on other sites More sharing options...
Maq Posted March 19, 2009 Share Posted March 19, 2009 w Quote Link to comment https://forums.phpfreaks.com/topic/150181-solved-foreach/#findComment-788715 Share on other sites More sharing options...
denoteone Posted March 19, 2009 Author Share Posted March 19, 2009 thanks everyone for your help I am learning more and more everyday! Quote Link to comment https://forums.phpfreaks.com/topic/150181-solved-foreach/#findComment-788721 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.