tags:

views:

91

answers:

5

I'm trying to merge these 2 arrays

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

into an array that looks like this

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

The tricky part is the blanks. I need to preserve them in place.

Thanks

+1  A: 

Try this:

$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];
}
Eric
A: 

here's my suggestion. It'll combine an arbitrary number of arrays according to what you described.

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;
}

Output:

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

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

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

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

)
cletus
seems like you're first accessing an element, then checking if it was `isset`
SilentGhost
Correct. If $arr1 has keys a,b,c and $arr2 has a,b,d, $ret will have keys a,b,c,d. d won't exist in $arr2 and thus the isset() check to put the empty string the OP wants.
cletus
I was more referring to the fact that if `$v` is not set, you'll get a Notice. also array summation in your first `foreach` loop didn't work for me. Have a look at the modification of your code that I've posted.
SilentGhost
A: 

I like cletus's approach, so I've just made sure it works :)

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;
}
SilentGhost
+2  A: 

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;

}
Kendall Hopkins
upvoting for code that has comments
Zak
+1  A: 
foreach (array_merge($arr1, $arr2) as $key => $val)
{
  $result[$key] = array("{$arr1[$key]}", "{$arr2[$key]}");
}

var_dump($result);
gpilotino