Guest Posted August 1, 2007 Share Posted August 1, 2007 Say I have some data in a string such as: 87628,1525,25234681 516288,70,774632 493667,65,483983 437419,73,1033755 333323,74,1172733 306434,70,739362 200033,55,166903 136325,78,1702545 133674,79,1909385 66914,85,3340303 74753,86,3901785 148905,76,1338969 6079,90,5559662 56730,69,697902 127836,63,375761 340252,66,514936 68274,58,241308 65056,62,351964 103862,59,262468 208125,50,105146 101665,48,85089 57181,61,312371 248675,35,24756 70938,53,138963 Where there are 20 or so groups of numbers, three numbers in each group. Say I wanted to select numbers 2 and 3 from group 4 (73 and 1033755). How would I go about doing this? I'm guessing I would either be using regex or str, but I just can't think at the moment =\. Note that these numbers would be different each time the script ran, and of different length, so I couldn't select the data from a certain position in the string or match the certain number. Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/62809-solved-selecting-data-from-a-set-of-numbers/ Share on other sites More sharing options...
AndyB Posted August 1, 2007 Share Posted August 1, 2007 It appears that your data is in the form number,number,number space number,number,number .... etc If so, you can use the explode() function to split the string at each space into an array with each element containing three numbers. And then you can explode each of those array elements at the comma to get the individual numbers. Quote Link to comment https://forums.phpfreaks.com/topic/62809-solved-selecting-data-from-a-set-of-numbers/#findComment-312671 Share on other sites More sharing options...
mrjcfreak Posted August 1, 2007 Share Posted August 1, 2007 i.e. $groups = explode(" ",$numbers); foreach($groups as $tempgroup) { $group[] = explode(",",$tempgroup); } Would give: group => array ( 0 => array(87628,1525,25234681), 1 => array(516288,70,774632), 2 => array(493667,65,483983) ) So you could access the second number from the third group by going: echo $group[2][1]; (remember arrays start at 0, so the second entry is [1], and the ninth sentry is [8] etc.) Quote Link to comment https://forums.phpfreaks.com/topic/62809-solved-selecting-data-from-a-set-of-numbers/#findComment-312677 Share on other sites More sharing options...
Guest Posted August 1, 2007 Share Posted August 1, 2007 Ok guys, thanks for your help. Just a question, could you also achieve the same usng preg_split? Quote Link to comment https://forums.phpfreaks.com/topic/62809-solved-selecting-data-from-a-set-of-numbers/#findComment-312684 Share on other sites More sharing options...
mrjcfreak Posted August 1, 2007 Share Posted August 1, 2007 Yes, but it's slower and you would still need two steps, so it has no advantages whatsoever unless your numbers are delimited by stuff that might change, or there being two spaces by accident instead of one. Quote Link to comment https://forums.phpfreaks.com/topic/62809-solved-selecting-data-from-a-set-of-numbers/#findComment-312687 Share on other sites More sharing options...
Guest Posted August 1, 2007 Share Posted August 1, 2007 Ah ok, thanks mrjcfreak. Quote Link to comment https://forums.phpfreaks.com/topic/62809-solved-selecting-data-from-a-set-of-numbers/#findComment-312693 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.