php 如何将foreach中的字符串组合成单个字符串PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13862083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:11:45  来源:igfitidea点击:

How to combine strings inside foreach into single string PHP

phpstringforeach

提问by Wakanina

I have code like this

我有这样的代码

$word = 'foo';
$char_buff = str_split($word);

foreach ($char_buff as $chars){
    echo var_dump($chars);
}

The output was

输出是

string(1) "f" 
string(1) "o" 
string(1) "o"

For some reasons, I want to make the output become only 1 string like this:

由于某些原因,我想让输出变成这样的只有 1 个字符串:

string(3) "foo"

I tried with this

我试过这个

$char.=$chars;
echo var_dump($char);

But it shows error Undefined variable: char.

但它显示错误Undefined variable: char

回答by Sampson

I'm going to assume that you have a good reason for splitting it up, only to put it back together again:

我将假设您有充分的理由将其拆分,然后再将其重新组合在一起:

$word = 'foo';
$result = "";
$char_buff = str_split($word);

foreach ($char_buff as $char){
    $result .= $char;
}

echo var_dump($result);

Which outputs the following:

输出以下内容:

string(3) "foo"

回答by Rosme

I would just use implode, much like this: $string = implode('', $char_buff);

我只会使用内爆,就像这样: $string = implode('', $char_buff);

回答by SeanWM

str_split()converts a string to an array. There's no need to use this function if you want to keep the whole word.

str_split()将字符串转换为数组。如果您想保留整个单词,则无需使用此功能。

回答by vectorialpx

So, why do you split it just to make it a string again?

那么,为什么要拆分它只是为了使它再次成为一个字符串?

$word='foo'
$char_buff = str_split($word);

$final = array();
foreach ($char_buff as $chars){
    $final[] = $chars;
}

var_dump( implode('', $final) );

回答by Bart Friederichs

Kind of strange to split a string, and then glue it together again, but here goes:

拆分一个字符串,然后再次将其粘合在一起有点奇怪,但是这里是:

$word='foo'
$char_buff = str_split($word);

// this is what is missing, you have to define a variable first
$newword = "";

foreach ($char_buff as $chars){
     $newword .= $chars;
}

echo var_dump($newword);

回答by Bart Friederichs

Sounds like you are looking for implode() http://php.net/manual/en/function.implode.php

听起来你正在寻找 implode() http://php.net/manual/en/function.implode.php

As for the code you posted $chars .= $char; is probably what you were trying to do

至于你发布的代码 $chars .= $char; 可能是你想要做的

回答by Tahir Yasin

<?php
$word = 'foo';
$char_buff = str_split($word);

// here is the trick
$length = count($char_buff);
$char_buff[$length] = $word;

foreach ($char_buff as $chars)
{
    echo var_dump($chars);
}
?>

回答by xreyc_developer22

Maybe some of you are looking for this answer. I think var_dump() is no longer necessary for this problem.

也许你们中的一些人正在寻找这个答案。我认为 var_dump() 不再需要解决这个问题。

<?php 
    if(isset($_POST['test'])){
        $result = '';
        for($x=1;$x<=4;$x++){
            $ans = $_POST['ans'.$x];
            $result .= $ans;
        } 
    echo $result; 
    }
?>

Here is the HTML

这是 HTML

<form role="form" method="post" action="<?php echo $url;?>">
 <input type="checkbox" name="ans1" value="A">A 
 <input type="checkbox" name="ans2" value="B">B 
 <input type="checkbox" name="ans3" value="C">C 
 <input type="checkbox" name="ans4" value="D">D 
 <input type="submit" name="test" value="Submit Answer">
</form>