我的数组是:
Array
(
[0] => 1
[1] => 4
[2] => 2
[3] => 5
[4] => 3
[5] => 6
[6] => 4
[7] => 7
)我使用以下代码:内爆( ",",$output );
但它返回如下: 1,4,2,5,3,6,4,7,6
我希望0与1和2与3和其他与“t”在他们之间。在他们两个都加上"ts“之后,应该加上一个逗号。如下:1 ts4,2 ts5,3ts6,4ts7
摘要:我不想用奇怪的逗号(我说的是内爆),而是用"ts“(1ts4,2ts5,3ts6,4ts7)。
发布于 2016-11-28 08:29:43
试下代码:-
$arr = [1,4,2,5,3,6,4,7];
$strArr= [];
for($i=0;$i<count($arr);$i=$i+2){
$strArr[] = "{$arr[$i]}ts{$arr[$i+1]}";
}
echo implode(',',$strArr);编辑过的
发布于 2016-11-28 08:23:30
您可以使用块块函数首先将数组拆分为各个部分,然后根据您的意愿将它们内爆。
发布于 2016-11-28 08:27:38
你可以这样做:-
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$arr = Array
(
0 => 1,
1 => 4,
2 => 2,
3 => 5,
4 => 3,
5 => 6,
6 => 4,
7 => 7
);
$new_array = array_chunk($arr,2); // break the array into sub-arrays of two-two values each
$my_string = ''; // an empty string
foreach ($new_array as $new_arra){ // iterate though the new chunked array
$my_string .= implode('ts',$new_arra).','; // implode the sub-array and add to the variable
}
echo trim($my_string,','); // echo variable产出:-1 ts4,2 ts5,3ts6,4tsz 7
https://stackoverflow.com/questions/40839742
复制相似问题