我有这样的代码
$assoc = ['name','lastname','pesel'];
$inputs = ['a', 'a', 'a', 'a', 'b', 'b', 'b', 'b',];
$counter = 0;
$internalcounter = 0;
$zero = 0;
$content = array();
while ($counter < sizeof($inputs))
{
if($internalcounter == sizeof($assoc))
{
$counter++;
$internalcounter = 0;
}
else
{
array_push($content, $inputs[$counter]);
$counter++;
$internalcounter++;
}
}
var_dump($content);
它创建了看起来像
的数组`array(6) { [0] => string(1) "a" [1] => string(1) "a" [2] => string(1) "a" [3] => string(1) "b" [4] => string(1) "b" [5] => string(1) "b" }`
但我想创建多维数组
Array ( [0] => Array ( [0] => "a" [1] => "a" [2] => "a" ) [1] => Array ( [0] => "b" [1] => "b" [2] => "b" ) )
如何在$ content中添加下一个数组?
答案 0 :(得分:2)
替代解决方案:
$assoc = ['name', 'lastname', 'pesel'];
$inputs = ['a', 'a', 'a', 'a', 'b', 'b', 'b', 'b'];
$content = array();
$i = 0;
foreach ($inputs as $key => $val) {
$key += 1;
if (($key < (sizeof($inputs)))) {
if (($key % (sizeof($assoc) + 1) == 0)) {
$content[++$i] = array();
} else {
$content[$i][] = $val;
}
}
}
var_dump($content);
答案 1 :(得分:0)
<?php
$assoc = ['name','lastname','pesel'];
$inputs = ['a', 'a', 'a', 'a', 'b', 'b', 'b', 'b'];
$counter = 0;
$internalcounter = 0;
$zero = 0;
$arrayIndex = 0;
$content = array();
$content[$arrayIndex] = array();
while ($counter < sizeof($inputs))
{
if ($internalcounter == sizeof($assoc))
{
$counter++;
$internalcounter = 0;
if ($counter < sizeof($inputs))
$content[++$arrayIndex] = array();
}
else
{
array_push($content[$arrayIndex], $inputs[$counter]);
$counter++;
$internalcounter++;
}
}
var_dump($content);