合并关联数组时计算空白

时间:2009-10-28 18:42:12

标签: php arrays syntax

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

$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")
);

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

由于

5 个答案:

答案 0 :(得分:2)


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;

}

答案 1 :(得分:1)

试试这个:

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

$keys = array();
$merged = array()

for($arr1 as $key=>$val)
{
    array_push($keys,$key);
}
for($arr2 as $key=>$val)
{
    array_push($keys,$key);
}
for($key in keys)
{
    $merged[$key] = array("","");
    if(isset($arr1[$key])) $merged[$key][0] = $arr1[$key];
    if(isset($arr2[$key])) $merged[$key][1] = $arr2[$key];
}

答案 2 :(得分:1)

foreach (array_merge($arr1, $arr2) as $key => $val)
{
  $result[$key] = array("{$arr1[$key]}", "{$arr2[$key]}");
}

var_dump($result);

答案 3 :(得分:0)

这是我的建议。它将根据您描述的内容组合任意数量的数组。

error_reporting(E_ALL | E_STRICT);
header('Content-Type: text/plain');

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

function combine() {
  $keys = array();
  foreach (func_get_args() as $arr) {
    if (is_array($arr)) {
      $keys += $arr;
    }
  }
  $keys = array_keys($keys);
  $values = array_pad(array(), count($keys), array());
  $ret = array_combine($keys, $values);
  foreach (func_get_args() as $arr) {
    foreach ($keys as $k) {
      $v = array_key_exists($k, $arr) ? $arr[$k] : '';
      array_push($ret[$k], $v);
    }
  }
  return $ret;
}

输出:

Array
(
    [a] => Array
        (
            [0] => 1
            [1] => 9
        )

    [b] => Array
        (
            [0] => 2
            [1] => 8
        )

    [c] => Array
        (
            [0] => 3
            [1] => 
        )

    [d] => Array
        (
            [0] => 
            [1] => 7
        )

)

答案 4 :(得分:0)

我喜欢cletus的方法,所以我确定它有效:)

function combine() {
    $keys = array();
    foreach (func_get_args() as $arr) {
        if (is_array($arr)) {
            $keys = array_merge($keys, array_keys($arr));
        }
    }
    $keys = array_unique($keys);
    $values = array_pad(array(), count($keys), array());
    $ret = array_combine($keys, $values);
    foreach (func_get_args() as $arr) {
        foreach ($keys as $k) {
            $v = '';
            if (array_key_exists($k, $arr)){
                $v = $arr[$k];
            }
            array_push($ret[$k], $v);
        }
    }
    return $ret;
}