1
$array = ['coke.','fanta.','chocolate.'];

foreach ($array as $key => $value) {
  if (strlen($value)<6) {
      $new[] =  $value." ".$array[$key+1];
  } else {
      $new[] = $value;
  }
}

This code doesn't have the desired effect, in fact it doesn't work at all. What I want to do is if an array element has string length less than 5, join it with the next element. So in this case the array should turn into this:

$array = ['coke. fanta.','chocolate.'];
Hasen
  • 11,710
  • 23
  • 77
  • 135

5 Answers5

2
$array = ['coke.','fanta.','chocolate.', 'candy'];
$new = [];

reset($array); // ensure internal pointer is at start
do{
    $val = current($array); // capture current value
    if(strlen($val)>=6):
        $new[] = $val; // long string; add to $new

    // short string. Concatenate with next value
    // (note this moves array pointer forward)
    else:
        $nextVal = next($array) ? : '';
        $new[] = trim($val . ' ' . $nextVal);
    endif;

}while(next($array));

print_r($new); // what you want

Live demo

BeetleJuice
  • 39,516
  • 19
  • 105
  • 165
1

With array_reduce:

$array = ['coke.', 'fanta.', 'chocolate.', 'a.', 'b.', 'c.', 'd.'];

$result = array_reduce($array, function($c, $i) {
    if ( strlen(end($c)) < 6 )
        $c[key($c)] .= empty(current($c)) ? $i : " $i";
    else
        $c[] = $i;
    return $c;
}, ['']);


print_r($result);

demo

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
0
<pre>
$array = ['coke.','fanta.','chocolate.'];
print_r($array);
echo "<pre>";
$next_merge = "";
foreach ($array as $key => $value) {
    if($next_merge == $value){
        continue;
    }
    if (strlen($value)<6) {
        $new[] =  $value." ".$array[$key+1];
        $next_merge = $array[$key+1];
    } else {
        $new[] = $value;
    }
}
print_r($new);
</pre>
Tejas Patel
  • 61
  • 1
  • 5
  • This will not work if the last element is short. Try adding `'pop'` after `'chocolate'` and running your code – BeetleJuice Jul 29 '17 at 06:31
  • I was trying to implement this code but didn't realise it had this issue. Not sure how pop would be used to fix this? Maybe a check to see if its the last element and if so operate 'continue'? – Hasen Jul 29 '17 at 06:53
  • You've added another answer? Why didn't you just edit this answer? – Hasen Jul 29 '17 at 07:26
0

You need to skip the iteration for the values that you have already added.

$array = ['coke.', 'fanta.', 'chocolate.'];
$cont = false;

foreach ($array as $key => $value) {
    if ($cont) {
        $cont = false;
        continue;
    }

    if (strlen($value) < 6 && isset($array[$key+1])) {
        $new[] = $value.' '.$array[$key+1];
        $cont = true;
    }
    else {
        $new[] = $value;
    }
}
print_r($new);
Bibek Shah
  • 419
  • 4
  • 19
0

Updated Code after adding pop after chocolate.

<pre>
$array = ['coke.','fanta.','chocolate.','pop'];
print_r($array);
echo "<br>";
$next_merge = "";
foreach ($array as $key => $value) {
    if($next_merge == $value){
        continue;
    }
    if (strlen($value)<6 && !empty($array[$key+1])) {
        $new[] =  $value." ".$array[$key+1];
        $next_merge = $array[$key+1];
    } else {
        $new[] = $value;
    }
}
print_r($new);
<pre>
Tejas Patel
  • 61
  • 1
  • 5