简体   繁体   中英

how inserting foreach loop all final result in a variable in php?

how inserting foreach loop all final result in a variable in php.

for example i have three array :

$myarray = ('a','b','c');
foreach($myarray as $myarray){
$text = $myarray;
}
echo $text;//i want echo abc but this code only print c.

i want taht code printing all final into a like $text and echo out a loop.

i know this code work nice but i want have all of the result out of loop

$myarray = ('a','b','c');
foreach($myarray as $myarray){
$text = $myarray;
echo $text;
}

please help.

thank you in advance from all friend.

This might be what you are looking for:

<?php
$myarray = ('a','b','c');
$text = '';
foreach($myarray as $element){
  $text .= $element;
}
echo $text;

An alternative would be something like that:

<?php
$myarray = ('a','b','c');
echo implode('', $myarray);
$text = [];
$myarray = ['a','b','c'];
foreach($myarray as $element) {
  $text[] = $element; // append a new element to the array $text
  // ok, kinda useless since $text==$myarray after the loop
  // but ....it's only an example
}
echo join(', ', $text); see // http://docs.php.net/join

You need the implode() :

<?php
$myarray = ('a','b','c');
echo implode('', $myarray);

Documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM