array_merge多维数组

I have 2 arrays - one is hard coded and the other is data retrieved from a database. I am trying to merge them but I'm having unexpected results.

This is the printed result of the first array:

Array
(
    [0] => Array
    (
        [product_image_one] => 
    )

    [1] => Array
    (
        [product_image_two] => 
    )

    [2] => Array
    (
        [product_image_three] => 
    )

    [3] => Array
    (
        [product_image_four] => 
    )
)

This is the printed result of the second array:

Array
(
    [0] => Array
    (
        [product_image_one] => ../wp-content/themes/dosco/images/products_images/355_product_image_one.jpg
    )
)

However, when I try to merge them like so:

$new_array = array_merge($base_image_array, $db_product_images);

The result is this:

Array
(
    [0] => Array
    (
        [product_image_one] => 
    )

    [1] => Array
    (
        [product_image_two] => 
    )

    [2] => Array
    (
        [product_image_three] => 
    )

    [3] => Array
    (
        [product_image_four] => 
    )

    [4] => Array
    (
        [product_image_one] => ../wp-content/themes/dosco/images/products_images/355_product_image_one.jpg
    )

)

What I want to achieve is this:

Array
(
    [0] => Array
    (
        [product_image_one] => ../wp-content/themes/dosco/images/products_images/355_product_image_one.jpg
    )

    [1] => Array
    (
        [product_image_two] => 
    )

    [2] => Array
    (
        [product_image_three] => 
    )

    [3] => Array
    (
        [product_image_four] => 
    )

)

I think the multidimensional nature of the arrays is confusing me.

The problem is that the array keys in both arrays are numeric. If you want one array to overwrite the other you need to get them into a state where the array keys are strings. You could do something like this:

$array_with_four = array(...); // The large array
$new_array_with_four = format_arrays($array_with_four);

$array_with_one = array(...); // The small array
$new_array_with_one = format_arrays($array_with_one);

$merged = array_merge($new_array_with_four, $new_array_with_one); // Your new results

function format_arrays($array) {
    $return_array = array(); // Blank array
    foreach ($array as $child_key => $child_data){
        $return_array[$child_key] = $child_data;
    }
    return $return_array;
}

There are probably better ways to loop through this with the php array functions - but this should get the job done!

clean up your num indexed array to kv array

<?php

$base_image_array = array
(
 array("product_image_one" => ""),
 array("product_image_two" => ""),
 array("product_image_three" => ""),
 array("product_image_four" => "")
);

$db_product_images = array
(
 array("product_image_one" => "../wp-content/themes/dosco/images/products_images/355_product_image_one.jpg"),
);

function kv($a){
    $keys = array_map(current, array_map(array_keys, $a));
    $values = array_map(current, array_map(array_values, $a));

    return array_combine($keys, $values);
}

$base_image_array  = kv($base_image_array);
$db_product_images = kv($db_product_images);

$new_array = array_merge($base_image_array, $db_product_images);
print_r($new_array);

output

Array
(
    [product_image_one] => ../wp-content/themes/dosco/images/products_images/355_product_image_one.jpg
    [product_image_two] =>
    [product_image_three] =>
    [product_image_four] =>
)

The solution was very similar to the answer by @Matthew R.

/* Base Array */
$base_image_array = array(
    array('product_image_one' => ''),
    array('product_image_two' => ''),
    array('product_image_three' => ''),
    array('product_image_four' => '')
);

/* Populated Array */
$db_product_images = array(
    array("product_image_one" => "../wp-content/themes/dosco/images/products_images/355_product_image_one.jpg")
);

function format_array($array) {
    $return_array = array();
    foreach ($array as $key => $value) {
        foreach ($value as $k => $v) {
            $return_array[$k] = $v;
        }
    }
    return $return_array;
}

$array1 = format_array($base_image_array);
$array2 = format_array($db_product_images);

$final_array = array_merge($array1, $array2);

echo '<pre>';
    print_r($final_array);
echo '</pre>';

The resulting output was like so:

Array
(
    [product_image_one] => ../wp-content/themes/dosco/images/products_images/355_product_image_one.jpg
    [product_image_two] => 
    [product_image_three] => 
    [product_image_four] => 
)

The final output was actually different to what I originally thought I needed.