solarisuser Posted February 22, 2008 Share Posted February 22, 2008 Hi All, I have data in an array like this "12345, 56789, KL5-1231231,KL5-1230502". Is there a function that I can use to say.... if string length of each piece of the array is less than 6, put it in this array, and the rest of the data entries go in another array. For example: $original_array = ("12345", "56789", "KL5-1231231","KL5-1230502"); some_function_here... $array1 = ("12345", "56789"); $array2 = ("KL5-1231231","KL5-1230502"); Thanks Link to comment https://forums.phpfreaks.com/topic/92383-php-function-for-strlen-in-array/ Share on other sites More sharing options...
drisate Posted February 22, 2008 Share Posted February 22, 2008 you can convert your aray to a string then explode by every (,) and loop using foreach Link to comment https://forums.phpfreaks.com/topic/92383-php-function-for-strlen-in-array/#findComment-473314 Share on other sites More sharing options...
duclet Posted February 22, 2008 Share Posted February 22, 2008 You don't really need a function. Couldn't you do something like the following: <?php $original_array = ("12345", "56789", "KL5-1231231","KL5-1230502"); $array1 = array(); $array2 = array(); foreach($original_array as $item) { if(isset($item[6])) { $array1[] = $item; } else { $array2[] = $item; } } ?> Of course you can use strlen($item) < 6 instead of what I have in the if statement but I like my way since it is a little faster. Link to comment https://forums.phpfreaks.com/topic/92383-php-function-for-strlen-in-array/#findComment-473340 Share on other sites More sharing options...
solarisuser Posted February 22, 2008 Author Share Posted February 22, 2008 very cool , thanks. i was looking for something not using strlen and tons of if statements... Link to comment https://forums.phpfreaks.com/topic/92383-php-function-for-strlen-in-array/#findComment-473349 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.