PeterPopper Posted November 11, 2009 Share Posted November 11, 2009 I need help using the explode function and having the results as numbers, not strings: //this is the first line of the textFiles being read from: 60,66,61,87,87,76,68,70,69,66,69,73,70 while(! feof($fileOpening)) { $externalDataRetrieved[] = rtrim(fgets($fileOpening)); } $theChartData = explode (",", $externalDataRetrieved[0]); The results in $theChartData are all strimngs and not numbers Quote Link to comment https://forums.phpfreaks.com/topic/181158-explode-to-numbers-instead-of-strings/ Share on other sites More sharing options...
Daniel0 Posted November 11, 2009 Share Posted November 11, 2009 $theChartData = array_map('intval', $theChartData); When you split a string it'll still be a string. PHP can't possibly know that you want the string "60" to be an integer, so if you want it to know it you'll have to tell it. Quote Link to comment https://forums.phpfreaks.com/topic/181158-explode-to-numbers-instead-of-strings/#findComment-955743 Share on other sites More sharing options...
knsito Posted November 11, 2009 Share Posted November 11, 2009 You want these functions: http://us3.php.net/manual/en/function.intval.php http://us3.php.net/manual/en/function.array-map.php here you go: $theChartData = array_map('intval',$theChartData); I need help using the explode function and having the results as numbers, not strings: //this is the first line of the textFiles being read from: 60,66,61,87,87,76,68,70,69,66,69,73,70 while(! feof($fileOpening)) { $externalDataRetrieved[] = rtrim(fgets($fileOpening)); } $theChartData = explode (",", $externalDataRetrieved[0]); The results in $theChartData are all strimngs and not numbers Quote Link to comment https://forums.phpfreaks.com/topic/181158-explode-to-numbers-instead-of-strings/#findComment-955750 Share on other sites More sharing options...
GoneNowBye Posted November 11, 2009 Share Posted November 11, 2009 #My fix may be quicker, but i'm not sure about efficency, *1 thats it that way php treats it as a float if it has a decimal, an integer if its whole. Quote Link to comment https://forums.phpfreaks.com/topic/181158-explode-to-numbers-instead-of-strings/#findComment-955757 Share on other sites More sharing options...
PeterPopper Posted November 11, 2009 Author Share Posted November 11, 2009 Thanks very much for the prompt and very helpful replies. This is what I did while waiting for a response: foreach ($theChartData as $key => $value) { $theChartDataNew[$key]= (int)str_replace('"','',$value); } knsito and Daniel0, that is the solution, it worked perfectly. Thanks. HavokDelta6, that is a cool tip that I have never seen. Thanks, mate. Quote Link to comment https://forums.phpfreaks.com/topic/181158-explode-to-numbers-instead-of-strings/#findComment-955771 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.