我在组合少数数组时遇到问题,第二个数组的索引必须存在但不能作为合并数组的值。
例如,这3个数组:
$a[0] = 'some A string';
$a[1] = 'some A string';
$a[2] = 'some A string';
$a[4] = 'some A string';
$a[6] = 'some A string';
$a[8] = 'some A string';
$a[10] = 'some A string';
$b[1] = 'some B string';
$b[3] = 'some B string';
$b[5] = 'some B string';
$b[7] = 'some B string';
$b[9] = 'some B string';
$b[11] = 'some B string';
$c[0] = 'some C string';
$c[1] = 'some C string';
$c[3] = 'some C string';
$c[6] = 'some C string';
$c[9] = 'some C string';
$c[12] = 'some C string';
$c[15] = 'some C string';
应改写为:
Array
(
[0] => some A string
[1] => some A string
[2] => some A string
[3] => blabla
[4] => some A string
[5] => blabla
[6] => some A string
[7] => blabla
[8] => some A string
[9] => blabla
[10] => some A string
[11] => blabla
[12] => blabla
[15] => blabla
)
Array
(
[0] => blabla
[1] => some B string
[2] => blabla
[3] => some B string
[4] => blabla
[5] => some B string
[6] => blabla
[7] => some B string
[8] => blabla
[9] => some B string
[10] => blabla
[11] => some B string
[12] => blabla
[15] => blabla
)
Array
(
[0] => some C string
[1] => some C string
[2] => blabla
[3] => some C string
[4] => blabla
[5] => blabla
[6] => some C string
[7] => blabla
[8] => blabla
[9] => some C string
[10] => blabla
[11] => blabla
[12] => some C string
[15] => some C string
)
可以有超过3个这样的阵列。 如何实现这样的结果,为每个用“blabla”填充的数组而不是从其他表中获取的值具有非常相同数量的键?
答案 0 :(得分:0)
编辑数组的动态长度
<?php
$a = array();
$a[0] = 'some A string';
$a[1] = 'some A string';
$a[2] = 'some A string';
$a[4] = 'some A string';
$a[6] = 'some A string';
$a[8] = 'some A string';
$a[10] = 'some A string';
end($a);
$last = key($a);
$returnarray = array();
for ($i = 0; $i <= $last; $i++) {
if(isset($a[$i])) {
$returnarray[] = $a[$i];
}else{
$returnarray[] = "blabla";
}
}
print_r($returnarray);
答案 1 :(得分:0)
首先,您需要获取密钥列表。
最简单的方法是创建超级数组并获取所有密钥。
$mega=array();
$mega=$a+$b+$c;
然后你想用array_keys();
获取所有键$keys=array_keys($mega);
然后你想要遍历你的密钥,找出哪些数组有哪些,哪些没有。如果您只有3个阵列,那么您可以手动迭代所有3个阵列。
完整版的工作代码:
<?php
$a[0] = 'some A string';
$a[1] = 'some A string';
$a[2] = 'some A string';
$a[4] = 'some A string';
$a[6] = 'some A string';
$a[8] = 'some A string';
$a[10] = 'some A string';
$b[1] = 'some B string';
$b[3] = 'some B string';
$b[5] = 'some B string';
$b[7] = 'some B string';
$b[9] = 'some B string';
$b[11] = 'some B string';
$c[0] = 'some C string';
$c[1] = 'some C string';
$c[3] = 'some C string';
$c[6] = 'some C string';
$c[9] = 'some C string';
$c[12] = 'some C string';
$c[15] = 'some C string';
$mega=array();
$mega=$a+$b+$c;
$keys=array_keys($mega);
$resulting_array=array();
foreach($keys as $key)
{
if(isset($a[$key]))
$resulting_array[0][$key]=$a[$key];
else
$resulting_array[0][$key]='blah';
}
foreach($keys as $key)
{
if(isset($b[$key]))
$resulting_array[1][$key]=$b[$key];
else
$resulting_array[1][$key]='blah';
}
foreach($keys as $key)
{
if(isset($c[$key]))
$resulting_array[2][$key]=$c[$key];
else
$resulting_array[2][$key]='blah';
}
var_dump($resulting_array);
输出:
array(3) {
[0]=>
array(14) {
[0]=>
string(13) "some A string"
[1]=>
string(13) "some A string"
[2]=>
string(13) "some A string"
[4]=>
string(13) "some A string"
[6]=>
string(13) "some A string"
[8]=>
string(13) "some A string"
[10]=>
string(13) "some A string"
[3]=>
string(4) "blah"
[5]=>
string(4) "blah"
[7]=>
string(4) "blah"
[9]=>
string(4) "blah"
[11]=>
string(4) "blah"
[12]=>
string(4) "blah"
[15]=>
string(4) "blah"
}
[1]=>
array(14) {
[0]=>
string(4) "blah"
[1]=>
string(13) "some B string"
[2]=>
string(4) "blah"
[4]=>
string(4) "blah"
[6]=>
string(4) "blah"
[8]=>
string(4) "blah"
[10]=>
string(4) "blah"
[3]=>
string(13) "some B string"
[5]=>
string(13) "some B string"
[7]=>
string(13) "some B string"
[9]=>
string(13) "some B string"
[11]=>
string(13) "some B string"
[12]=>
string(4) "blah"
[15]=>
string(4) "blah"
}
[2]=>
array(14) {
[0]=>
string(13) "some C string"
[1]=>
string(13) "some C string"
[2]=>
string(4) "blah"
[4]=>
string(4) "blah"
[6]=>
string(13) "some C string"
[8]=>
string(4) "blah"
[10]=>
string(4) "blah"
[3]=>
string(13) "some C string"
[5]=>
string(4) "blah"
[7]=>
string(4) "blah"
[9]=>
string(13) "some C string"
[11]=>
string(4) "blah"
[12]=>
string(13) "some C string"
[15]=>
string(13) "some C string"
}
}
答案 2 :(得分:0)
这就是你想要的。
$a[0] = 'some A string';
$a[1] = 'some A string';
$a[2] = 'some A string';
$a[4] = 'some A string';
$a[6] = 'some A string';
$a[8] = 'some A string';
$a[10] = 'some A string';
$arr=[];
for($i=0; $i<=15; $i++){
if(!isset($a[$i]))
$a[$i]='blabla';
array_push($arr, $a[$i]);
}
echo "<pre>";
print_r($arr);
echo "</pre>";