2011-06-29 28 views
0

にPHPの文字列の配列をconcating I持っている文字列の配列を受け取り、2つの文字列にそれを分割し、次のコード、:5インデックス配列で今2つの以上の文字列

$key2 = $count = count($textArray); 
$key = 0; 
while ($key2 >= $count/2){ 
    $this -> text1 = $this-> text1 . $textArray[$key]; 
    $this -> text2 = $textArray[$key2] . $this-> text2; 
    $key++; 
    $key2--; 
} 

Iそれを分割する3-2、私はそれを2-3にしたいので、text1で、私は2つの文字列をconcateするだけです

これは非常に簡単なことですが、私はできません。

答えて

0

Array 
(
    [0] => ab 
    [1] => cdf 
) 
0

これはいかがですか?

<?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"; 
?> 

チェックこのアウト:

$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); 

出力::私は私が正しくあなたの質問を読んで期待しhttp://php.net/manual/en/function.array-chunk.php

+2

は 'はGlobal'を使用しないでください。代わりに、あなたの無名関数を 'function($ x)use($ text1){$ text1。= $ x;}'と書くことができます。 – Yoshi

+0

@ Yoshi:ありがとう! :-) ..私は 'use(..)'について本当に知りませんでした。 – SuperSaiyan

関連する問題