首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将php中的字符串数组连接成2个或更多个字符串

将php中的字符串数组连接成2个或更多个字符串
EN

Stack Overflow用户
提问于 2011-06-29 17:22:44
回答 2查看 265关注 0票数 0

我有以下代码,它接受一个字符串数组,并将其拆分为两个字符串:

代码语言:javascript
复制
$key2 = $count = count($textArray);
$key = 0;
while ($key2 >= $count/2){
    $this -> text1 = $this-> text1 . $textArray[$key];
    $this -> text2 = $textArray[$key2] . $this-> text2;
    $key++;
    $key2--;
}

现在在一个5索引的数组上,我可以将它拆分成3-2,我想让它变成2-3,所以在text1上,我只连接2个字符串,

这是非常简单的事情,但我做不到。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-06-29 17:36:45

我希望我没看错你的问题:

代码语言:javascript
复制
$strings = array('a', 'b', 'c', 'd', 'f');

$new = array_map(function ($arr) {
    return implode('', $arr);
}, array(array_splice($strings, 0, floor(sizeof($strings) / 2)), $strings));

print_r($new);

输出:

代码语言:javascript
复制
Array
(
    [0] => ab
    [1] => cdf
)
票数 0
EN

Stack Overflow用户

发布于 2011-06-29 17:34:14

这个怎么样?

代码语言:javascript
复制
<?php
$arr = array("qwe","rty","asd","zxc","fgh","xxx","abvah");

$arrNew = array_chunk($arr,ceil(count($arr)/2.0));
$text1 = "";
$text2 = "";

/*foreach ($arrNew[0] as $arr1){
    $text1 .= $arr1;
}
foreach ($arrNew[1] as $arr2){
    $text2 .= $arr2;
}*/
array_map(function($x) use($text1){$text1 .= $x;},$arrNew[0]);
array_map(function($x) use($text2){$text2 .= $x;},$arrNew[1]);

print "$text1\n$text2\n";
?>

看看这个:http://php.net/manual/en/function.array-chunk.php

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6518340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档