我有这个数组:
$values = Array
(
[0] => foo-1
[1] => foo-2
[2] => foo-3
[3] => foo-4
[4] => foo-5
[5] => foo-6
[6] => foo-7
[7] => foo-8
[8] => foo-9
[9] => foo-10
[10] => foo11
[...]
[30] => foo-31
[31] => foo-32
)
我需要将所有13个值的数组分解,然后得到:
echo "foo-1, foo-2, foo-3, foo-4, foo-5, foo-6 [...], foo-13";
echo "foo-14, foo-15, foo-16, foo-17 [...], foo-26";
echo "foo-27, foo-28, foo-29, foo-30, foo-31, foo-32";
所以,这是我的代码:
$nVal = count($values);
$nbTab = $nVal / 13;
$nbTab = round($nbTab, 0, PHP_ROUND_HALF_UP);
for ($cpt = 0; $cpt <= $nbTab-1; $cpt++) {
$line = "";
if($cpt == 0)
$cptEntete = $cpt*12;
else
$cptEntete = ($cpt*12)+1;
echo "Cpt entete :".$cptEntete;
if($cpt+1 == $nbTab){
$jusque = $nVal;
}
else{
if($cpt == 0)
$jusque = 12;
else
$jusque = 12*($cpt+1);
}
for($cptEntete;$cptEntete <= $jusque;$cptEntete++){
$line.= $cptEntete." ,";
}
echo $line."<br />";
问题在于我没有按行到达我的13个值的好行:(
能帮帮我吗?
答案 0 :(得分:0)
试试:
$values = array( /* your data */ );
$parts = array_chunk($values, 13);
foreach ($parts as $part) {
echo implode(', ', $part);
}
答案 1 :(得分:0)
$myArray = [];
for($i = 1; $i != 33; $i++)
$myArray[] = "foo-$i";
$arrays = array_chunk($myArray, 13);
foreach($arrays as $array)
echo implode(', ', $array)."\n";
答案 2 :(得分:0)
echo join(
'<br>', // or whatever
array_map(
function (array $chunk) { return join(', ', $chunk); },
array_chunk($values, 13)
)
);
这个chunks你的数组分别为13个,用,
连接每个chunk的所有元素,然后按<br>
加入每个chunk(用你想要的任何东西替换它)。 / p>