mac_gabe Posted July 9, 2011 Share Posted July 9, 2011 Hello again, I have an array of numbers, like this: $array = (1,2,4,5,6,8,10,12,15,73,99,102,1901,1915,1922,1923,1926,1942,1943,1944,1980,1981,1983,1999,2001,2002,2003,2011) which I want to split it into 2 arrays, one with lengths (small numbers) and one with dates (1901+). So: $array1 = (1,2,4,5,6,8,10,12,15,73,99,102) $array2 = (1901,1915,1922,1923,1926,1942,1943,1944,1980,1981,1983,1999,2001,2002,2003,2011) both arrays will then be reverse sorted with biggest numbers at the top. The smallest and largest number of $array1 will change over time - but starts with the first value, and the largest is less than 1000 Similarly with the dates, but they start at around 1890 or 1910 and finish with the current year. I can see how to do this by dividing a string with preg_replace at the 1st four-figure number - ie something like (.*?)(\d\d\d\d)(.*) as a search term, - since the data comes from a string anyway maybe this is the best way of doing it. But I thought there might be a neater method of just splitting the array itself with a built-in function, which I didn't know about. If the only way is to use regex while looping through the array, then I might as well just stick to my known method and use it on the string. Thanks for any help as always. Quote Link to comment https://forums.phpfreaks.com/topic/241508-split-array-into-2-beginner-level/ Share on other sites More sharing options...
QuickOldCar Posted July 9, 2011 Share Posted July 9, 2011 <?php $array = array(1,2,4,5,6,8,10,12,15,73,99,102,1901,1915,1922,1923,1926,1942,1943,1944,1980,1981,1983,1999,2001,2002,2003,2011); foreach ($array as $temp_string) { $string_length = strlen(trim($temp_string)); if($string_length <= 3){ $array1[] = trim($temp_string); } else { $array2[] = trim($temp_string); } } echo "Array 1 <br />"; //$array1 = array_reverse($array1); rsort($array1); foreach($array1 as $value1){ echo $value1."<br />"; } echo "Array 2 <br />"; //$array2 = array_reverse($array2); rsort($array2); foreach($array2 as $value2){ echo $value2."<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241508-split-array-into-2-beginner-level/#findComment-1240576 Share on other sites More sharing options...
mac_gabe Posted July 9, 2011 Author Share Posted July 9, 2011 Very nice, many thanks! Meanwhile I hammered out my "old way" of doing it with preg_replace (which is in fact very simple since I was already using preg_replace then explode to get from original string to array, so I just had to change the search term and do it twice), but I prefer your method as I can keep it all in an array, so it's neater, plus I get to use a new function strlen, which I had seen before but never used. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/241508-split-array-into-2-beginner-level/#findComment-1240631 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.