Elements combination of same array in PHP -
in project, have receive string user (in textarea). string
converted array
. problem that, character length must minimum of 3, in following array next element should joined current 1 if character length less 3. how perform in php
.
a[0]=>this a[1]=>is a[2]=>an a[3]=>example a[4]=>array.
output should be:
a[0]=>this a[1]=>isan a[2]=>example a[3]=>array.
just try with:
$input = ['this', 'is', 'an', 'example', 'array.']; $output = []; $part = ''; foreach ($input $value) { $part .= $value; if (strlen($part) > 3) { $output[] = $part; $part = ''; } }
output:
array (size=4) 0 => string 'this' (length=4) 1 => string 'isan' (length=4) 2 => string 'example' (length=7) 3 => string 'array.' (length=6)
Comments
Post a Comment