Jump to content

[SOLVED] Selecting data from a set of numbers...


Recommended Posts

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.

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.

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.)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.