开发者

Counting blanks when merging assoctiative arrays

开发者 https://www.devze.com 2022-12-09 13:26 出处:网络
I\'m trying to merge these 2 arrays $ar开发者_高级运维r1 = array(\'a\' => \"1\", \'b\' => \"2\", \'c\' => \"3\");

I'm trying to merge these 2 arrays

$ar开发者_高级运维r1 = 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



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;

}


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


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

var_dump($result);


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
        )

)


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

精彩评论

暂无评论...
验证码 换一张
取 消