1

I have an array filled with these strings for example:

"1", "2", "3", "4", "5";

I have an foreach() loop, that loops through these items. It outputs all the items. Now, it outputs this:

1, 2, 3, 4, 5

But I want to output this:

1, 12, 123, 1234, 12345

How do I do this? Because the prev() function only applies to the one before the current item.

My code:

$array = array("1", "2", "3", "4", "5");
foreach ($array as $item){
    echo $item;
}
5
  • please provide your code. Commented Apr 13, 2016 at 12:21
  • 1
    Concat them as a string for each iteration? Commented Apr 13, 2016 at 12:22
  • for loop + array_slice() Commented Apr 13, 2016 at 12:22
  • 1
    foreach($data as $key => $value) { echo implode(array_slice($data, 0, $key+1)), PHP_EOL; } or foreach(array_keys($data) as $key) { echo implode(array_slice($data, 0, $key+1)), PHP_EOL; } Commented Apr 13, 2016 at 12:24
  • Don't you have any idea? It's an easy task. Commented Apr 13, 2016 at 12:25

6 Answers 6

6
$include = "";
foreach($array as $item) {
    echo $include . $item;
    $include .= $item;
}

Separators version:

$string = "";
$include = "";
foreach($array as $item) {
    $string .= $include . $item . ", ";
    $include .= $item;
}
echo rtrim($string, ', \t\n');
Sign up to request clarification or add additional context in comments.

7 Comments

Really want to mark it accepted, but I have to wait five more minutes haha. And I have been busy with PHP for some time now, that's why I got very frustrated for not knowing this :p
Sometimes the simplest solutions just don't come to mind, I almost over complicated this myself.
What about separators ?
I'm sure he's smart enough to figure out separators :) - but, just for you ;)
Also for reference, the code he provided didn't have separators ;)
|
2

Concatenate the current item each time through:

$output = '';
foreach ($array as $item){
    $output .= $item;
    echo $output;
}

For a comma separated list you can create an array and implode:

$items = '';
foreach ($array as $item){
    $items .= $item;
    $output[] = $items;
}
echo implode(', ', $output);

2 Comments

What about separators... ?
Yay ! So much better :)
1
$array = array("1", "2", "3", "4", "5");
$value = '';

foreach ($array as $item){
    $value .= $item;
    echo $value;
}

2 Comments

Why should the OP try this? A good answer will always have an explanation (and is never just code only) of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
@MathieuLescaudron If you take note, please add explainations of what you do, not just code. By the way this does not provide the expected output... Missing separators etc...
1

concatination in php be executed by .(point)

$array = array("1", "2", "3", "4", "5");
$temp = '';
$output = '';
foreach ($array as $item){
    $temp .= $item;
    $output .= $temp . ',';
}
echo rtrim($output,',');

1 Comment

Why should the OP try this? A good answer will always have an explanation (and is never just code only) of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
0
$array = array("1", "2", "3", "4", "5");
$concatItem = "";
foreach ($array as $item){
    $concatItem = $concatItem.$item;
    echo $concatItem;
}

2 Comments

Why should the OP try this? A good answer will always have an explanation (and is never just code only) of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
What about separators ?
0

use like this way...

<?php

$array = array("1", "2", "3", "4", "5");
$separate = $array;
$series =' ';


foreach ($array as $item){
    $series .=$item;
    echo $series;
    if (next($separate )) {
        echo ','; // Add comma for all elements instead of last
    }
}

?>

Output.. enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.