"2", 'c' => "3"); $arr2 = array('a' => "9", 'b' => "8"-6ren">
gpt4 book ai didi

php - 合并关联数组时计算空白

转载 作者:可可西里 更新时间:2023-11-01 01:10:46 25 4
gpt4 key购买 nike

我正在尝试合并这两个数组

$arr1 = array('a' => "1", 'b' => "2", 'c' => "3");
$arr2 = array('a' => "9", 'b' => "8", 'd' => "7");

变成这样的数组

$arr1 = array(  
'a' => array("1", "9"),
'b' => array("2", "8"),
'c' => array("3", ""),
'd' => array("", "7")
);

棘手的部分是空白。我需要将它们保存到位。

谢谢

最佳答案


function merge()
{

$array_of_arrays = func_get_args();

//get all the unique keys
$final_array_keys = array_keys( call_user_func_array( "array_merge", $array_of_arrays ) );

//make final array
$final_array = array();
foreach( $final_array_keys as $key ) {
foreach( $array_of_arrays as $current_array ) {
$final_array[$key][] = array_key_exists( $key, $current_array ) ? $current_array[$key] : "";
}
}
return $final_array;

}

关于php - 合并关联数组时计算空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1639278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com