EchoFool Posted June 19, 2009 Share Posted June 19, 2009 Hey I have a string like this: $string = '12,14,37,124,5,77,35,74'; Now each number represents what the result was for each day. So 12 was yesterday, 14 was day before than and so on. What im trying to do is make a script then to pick a day of the list and display that day... so say i wanted to display the result for 3 days ago... That would be 37. How would i do it in a while loop.. to cycle through it and then only echo the result when $var in the count of the while loop equalled the $day = 3... sumin like this <?php $getday = 3; $var = 1; While ( some thing ) { If($var == $getday){ Echo $result; } $var = $var + 1; } ?> How ever if there is a more efficient way id be interested to know your view to it.. the amount of numbers in the list could be hundreads just to add. Quote Link to comment https://forums.phpfreaks.com/topic/162911-solved-string-seperation-and-assignment/ Share on other sites More sharing options...
Mark Baker Posted June 19, 2009 Share Posted June 19, 2009 $string = '12,14,37,124,5,77,35,74'; $daysAgo = 3; $result = getDaysAgo($string,$daysAgo); echo $result; function getDaysAgo($str,$days) { $strArray = explode(",",$str); $days--; return $strArray[$days]; } Quote Link to comment https://forums.phpfreaks.com/topic/162911-solved-string-seperation-and-assignment/#findComment-859583 Share on other sites More sharing options...
taquitosensei Posted June 19, 2009 Share Posted June 19, 2009 This should do it. $string = '12,14,37,124,5,77,35,74'; $array=explode(",", $string); $day=3; $number=$string($day-1); // arrays start at 0 so day 1 would be 0, day 2 would be 1, etc Quote Link to comment https://forums.phpfreaks.com/topic/162911-solved-string-seperation-and-assignment/#findComment-859585 Share on other sites More sharing options...
EchoFool Posted June 19, 2009 Author Share Posted June 19, 2009 Thanks guys! Quote Link to comment https://forums.phpfreaks.com/topic/162911-solved-string-seperation-and-assignment/#findComment-859586 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.