Jump to content

php function for strlen in array?


solarisuser

Recommended Posts

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.