PHP combine two multi-dimensional arrays

前端 未结 2 1627
伪装坚强ぢ
伪装坚强ぢ 2021-01-13 09:57

I am trying to use array_combine to combine two multi-dimensional arrays, but somehow not doing it correctly.

Here is array1:

Array(
    [Nov 18, 2         


        
相关标签:
2条回答
  • 2021-01-13 10:26

    The result you're looking for looks really custom to me. That is to say, I don't know of any built-in PHP array functions that would do that. However, I did write a custom function for you. Warning: it is very tailored to this occasion and thus probably not very reusable.

    function cust_array_merge(array $array1, array $array2)
    {
        $merged = array();
        // loop through main array
        foreach ($array1 as $key => $val) {
            // check if $array2 has the same index
            if (array_key_exists($key, $array2)) {
                // reset $array1's indexes to $array2's values
                foreach ($array2[$key] as $subKey => $subVal) {
                    if (array_key_exists($subKey, $array1[$key])) {
                        $tempVal = $array1[$key][$subKey];
                        unset($array1[$key][$subKey]);
                        $array1[$key][$subVal] = $tempVal;
                    }
                }
                $merged = $array1;
            }
        }
        return $merged;
    }
    
    0 讨论(0)
  • 2021-01-13 10:35

    array_merge_recursive gets you very close (your "key" is in index 1 of leaf arrays, and your value is in index 0). So do it in two steps: first get the merged array, then fiddle with the branches to get it right.

    // This is a callback for array_map() which should have some more generic uses.
    // array(array('k', 'v'), ...) -> array('k' => 'v', ...)
    function flatten_branches($branches) {
        $newleaves = array();
        foreach ($branches as $leaf) {
            $newleaves[$leaf[0]] = $leaf[1];
        }
        return $newleaves;
    }
    
    function merge_flatten_branches($karray, $varray) {
        //$karray has the key-leaves, and $varray has the value-leaves
        $m1 = array_merge_recursive($karray, $varray);
        return array_map('flatten_branches', $m1);
    }
    
    $merged = merge_flatten_branches($array2, $array1);
    print_r($merged);
    

    Just for fun, here are two more approaches. Both of these are a bit slower than merge_flatten_branches, but illustrate some useful array concepts. (In other more functional-flavored languages than php, these might be preferred.)

    function merge_flatten_branches_reduce($karray, $varray) {
        //$karray has the key-leaves, and $varray has the value-leaves
        $m1 = array_merge_recursive($karray, $varray);
        return array_map('flatten_branches_reduce', $m1);
    }
    
    function merge_flatten_branches_add($karray, $varray) {
        //$karray has the key-leaves, and $varray has the value-leaves
        $m1 = array_merge_recursive($karray, $varray);
        return array_map('flatten_branches_add', $m1);
    }
    
    // The functions below are callbacks for the two above.
    
    function array_add($a1, $a2) {
        return $a1+$a2;
    }
    
    function flatten_leaf($leaf) {
        return array($leaf[0] => $leaf[1]);
    }
    
    function flatten_branches_add($branches) {
        $leaves = array_map('flatten_leaf', ($branches));
        $finalleaves = array();
        foreach ($leaves as $leaf) {
            $finalleaves += $leaf;
        }
        return $finalleaves;
    }
    
    
    function flatten_branches_reduce($branches) {
        $l = array_map('flatten_leaf', ($branches));
        return array_reduce($l, 'array_add', array());
    }
    
    0 讨论(0)
提交回复
热议问题