Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate All Values of an Array

I have a loop like this below:

foreach ($header as $i) {
    $i += $i;
}

I am trying to load a variable ($i) and then outside of that loop echo that variable like this below:

echo $i;

However it always returns 0;

Is it possible to get it to return the value that it created in the loop?

like image 456
bryan sammon Avatar asked Jan 26 '11 19:01

bryan sammon


2 Answers

You can use implode() to combine all the values.

$all = implode('', $header);

http://php.net/implode

like image 191
Jonah Avatar answered Oct 09 '22 14:10

Jonah


$i is re-assigned every time the loop iterates.

create a variable outside the loop, add to it during the loop, and echo again outside of it.

$outside_var = 0;

foreach ($header as $i) {
    $outside_var += $i;
}

echo $outside_var;
like image 42
jondavidjohn Avatar answered Oct 09 '22 14:10

jondavidjohn