我正在使用codeigniter php编程,我有3个数组,如
array(1) {
[1506546000]=>
object(stdClass)#34 (1) {
["purchases"]=>int(120)
}
}
array(1) {
[1506546000]=>
object(stdClass)#32 (1) {
["exchange_items"]=>int(10)
}
}
array(3) {
[1506546000]=>
object(stdClass)#40 (1) {
["production_system"]=>int(16050)
}
[1506373200]=>
object(stdClass)#33 (1) {
["production_system"]=>int(2250)
}
[1506805200]=>
object(stdClass)#39 (1) {
["production_system"]=>int(150)
}
}
我需要将它们合并到一个数组中,并将键与此类
合并array(1) {
[1506546000]=>
["purchases"]=>int(120)
["exchange_items"]=>int(10)
["production_system"]=>int(16050)
}
array(1) {
[1506373200]=>
["purchases"]=>null
["exchange_items"]=>null
["production_system"]=>int(2250)
}
array(1) {
[1506373200]=>
["purchases"]=>null
["exchange_items"]=>null
["production_system"]=>int(150)
}
我使用array_merge_recursive()
,但它并没有完全满足我的需要,
所以有任何帮助吗?
答案 0 :(得分:1)
我不认为有一个PHP内置函数可以做你想做的事情,但我可能错了。你可以这样做:
<?php
$final_arr = array();
function setup()
{
return array(
'purchases' => NULL,
'exchange_items' => NULL,
'production_system' => NULL
);
}
$arr1 = array(
1506546000 => (object) array(
'purchases' => 120
)
);
$arr2 = array(
1506546000 => (object) array(
'exchange_items' => 10
)
);
$arr3 = array(
1506546000 => (object) array(
'production_system' => 16050
),
1506373200 => (object) array(
'production_system' => 2250
),
1506805200 => (object) array(
'production_system' => 150
)
);
foreach( array( $arr1, $arr2, $arr3 ) as $array )
{
foreach( $array as $k => $v )
{
if( ! array_key_exists( $k, $final_arr ) )
$final_arr[$k] = setup();
foreach($v as $k2 => $v2)
{
$final_arr[$k][$k2] = $v2;
}
}
}
echo '<pre>';
print_r( $final_arr );
echo '</pre>';