开发者

Transpose 2 associative arrays of unequal size to form an associative 2d array

开发者 https://www.devze.com 2023-01-17 08:37 出处:网络
Example: $arr1 = array(\'a\' => "1", \'b\' => "2", \'c\' => "3", \'z\' => "4");

Example:

$arr1 = array('a' => "1", 'b' => "2", 'c' => "3", 'z' => "4");
$arr2 = array('a' => "9", 'b' => "8", 'c' =&g开发者_如何学JAVAt; "7", 'd' => "6", 'e' => "5");

Output:

$result = array(
    'a' => array( 'f1' => "1", 'f2' => "9"),
    'b' => array( 'f1' => "2", 'f2' => "8"),
    'c' => array( 'f1' => "3", 'f2' => "7"),
    'd' => array( 'f1' => "0", 'f2' => "6"),
    'e' => array( 'f1' => "0", 'f2' => "5"),
    'z' => array( 'f1' => "4", 'f2' => "0"),
);

The size of $arr1 can be '>', '<' or '=' size of $arr2


I think this it,

$arr1 = array('a' => "1", 'b' => "2", 'c' => "3", 'z' => "4");

$arr2 = array('a' => "9", 'b' => "8", 'c' => "7", 'd' => "6", 'e' => "5");


foreach($arr1 as $key => $value){
    $a[$key]['f1'] = $value;
}

foreach($arr2 as $key => $value){
    $b[$key]['f2'] = $value;
}

$c = array_merge_recursive($a, $b);
foreach($c as $key => $value){
    $result[$key]['f1'] = (array_key_exists('f1', $value)) ? $value['f1']: 0;
    $result[$key]['f2'] = (array_key_exists('f2', $value)) ? $value['f2']: 0;
}

echo "<pre>".print_r ($result, true);

the output:

Array
(
    [a] => Array
        (
            [f1] => 1
            [f2] => 9
        )

    [b] => Array
        (
            [f1] => 2
            [f2] => 8
        )

    [c] => Array
        (
            [f1] => 3
            [f2] => 7
        )

    [z] => Array
        (
            [f1] => 4
            [f2] => 0
        )

    [d] => Array
        (
            [f1] => 0
            [f2] => 6
        )

    [e] => Array
        (
            [f1] => 0
            [f2] => 5
        )

)


array_merge_recursive() should work: http://php.net/manual/en/function.array-merge-recursive.php

otherwise it's simple enough wo implement in a few lines: (Unless you really need the "fn" indices.)

function my_merge(){

    $result = array();
    foreach(func_get_args() as $a)
        foreach($a as $index => $value)
            $result[$index][] = $value;         

}


Try this:

$keys = array_unique(array_merge(array_keys($arr1), array_keys($arr2)));
$values = array_map(function($key) use ($arr1, $arr2) {
    return array('f1' => isset($arr1[$key]) ? $arr1[$key] : "0",
                 'f2' => isset($arr2[$key]) ? $arr2[$key] : "0"); }
    , $keys);
$result = array_combine($keys, $values);
var_dump($result);

Some explanation:

  • At first get an array $keys of all unique keys of both arrays.
  • Then for each key the corresponding array of values is then generated.
  • At last the array of keys and the array of values are combined.

But this does currently only work with two arrays.


Edit    Here’s one that works with an arbitrary number of arrays:

$arrays = array($arr1, $arr2);
$keys = array_unique(call_user_func_array('array_merge', array_map('array_keys', $arrays)));
$values = array_map(function($key) use ($arrays) {
    return array_combine(array_map(function($key) {
        return 'f'.($key+1);
    }, array_keys($arrays)),
    array_map(function($array) use ($key) {
        return isset($array[$key]) ? $array[$key] : "0";
    }, $arrays));
}, $keys);
$result = array_combine($keys, $values);
var_dump($result);

Some explanation:

  • Put all arrays in an array $arrays.
  • For each array in $arrays, call array_keys on it to get the keys, merge these arrays and get an array $keys of all unique keys.
  • For each key in $keys, check if the key exists in the array of arrays $arrays.
  • Combine the keys and values.


$arr1 = array('a' => "1", 'b' => "2", 'c' => "3", 'z' => "4");

$arr2 = array('a' => "9", 'b' => "8", 'c' => "7", 'd' => "6", 'e' => "5");

function combineArray($arr1, $arr2) {
    if (is_array($arr1) && is_array($arr2)) {
        $rArr = array();
        $steps = max ( count($arr1),count($arr2));
     $ak1 = array_keys($arr1);
     $ak2 = array_keys($arr2);

        for ($i=0;$i<$steps;$i++) {
                if (!isset($rArr[$i])) $rArr[$i]=array();
                $rArr[$i]['f1'] = (isset($arr1[$ak1[$i]])) ? $arr1[$ak1[$i]]: '0';
                $rArr[$i]['f2'] = (isset($arr2[$ak2[$i]])) ? $arr2[$ak2[$i]]: '0';
        }
        return $rArr;

    }else {
        return false;
    }
}

echo "<pre>".print_r (combineArray($arr1, $arr2),true);

might work :)


  1. Run through the first array, store the f1 values from the array and set the default 0 for all f2 values.
  2. Run through the second array, store the f2 values from the array and only set the default 0 for the respective f1 value if it is not already declared.
  3. Use ksort() if desired to alphabetize the first level keys.

Code: (Demo)

$array1 = ['a' => "1", 'b' => "2", 'c' => "3", 'z' => "4"];
$array2 = ['a' => "9", 'b' => "8", 'c' => "7", 'd' => "6", 'e' => "5"];

$result = [];
foreach ($array1 as $k => $v) {
    $result[$k] = ['f1' => $v, 'f2' => "0"];
}
foreach ($array2 as $k => $v) {
    $result[$k] = ['f1' => $result[$k]['f1'] ?? "0", 'f2' => $v];
}

var_export($result);
0

精彩评论

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