PHP combine two multi-dimensional arrays

前端 未结 2 1630
伪装坚强ぢ
伪装坚强ぢ 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;
    }
    

提交回复
热议问题