如何将两个数组值合并到一个数组中并保存在数据库中

How can merge two arrays values in one array and save in database

我正在处理 WordPress 上传元字段。当用户上传图像时,图像的尺寸是二维的,一个是 "thumb",一个是 "big",并且它们的大小调整得非常完美。我使用不同的元键将两个图像维度路径保存在数据库中,例如:

用于缩略图 wpc_resize_thumb_images 和大图像 wpc_resize_big_images.

当我在数据库中保存图像路径时,它保存完美。

这是我将它们保存在数据库中的代码:

对于大图

$product_img_path[$count]['wpc_resize_big_img'] = $upload_dir['url'].'/'.$resize_img_name;
update_post_meta($post->ID, 'wpc_resize_big_images', $product_img_path);

在数据库中它是这样保存的:

meta_key

wpc_resize_big_images

meta_value

a:2:{i:1;a:1:{s:18:"wpc_resize_big_img";s:79:"http://localhost/test/wp-content/uploads/2015/06/Wallpaper_55-500x375.jpg";}i:2;a:1:{s:18:"wpc_resize_big_img";s:79:"http://localhost/test/wp-content/uploads/2015/06/Wallpaper_51-500x333.jpg";}}

和缩略图

$product_img_path[$count]['wpc_resize_thumb_img'] = $upload_dir['url'].'/'.$resize_img_name;
update_post_meta($post->ID, 'wpc_resize_thumb_images', $product_img_path);

在数据库中它是这样保存的:

meta_key

wpc_resize_thumb_images

meta_value

a:2:{i:1;a:1:{s:20:"wpc_resize_thumb_img";s:79:"http://localhost/test/wp-content/uploads/2015/06/Wallpaper_55-212x159.jpg";}i:2;a:1:{s:20:"wpc_resize_thumb_img";s:79:"http://localhost/test/wp-content/uploads/2015/06/Wallpaper_51-212x141.jpg";}}

当我打印它们时,它们会显示如下结果:

大图像

$wpc_resize_big_images = get_post_meta($post->ID, 'wpc_resize_big_images', true);
echo "<pre>";
    print_r($wpc_resize_big_images);
echo "</pre>";

结果是

Array
(
    [1] => Array
        (
            [wpc_resize_thumb_img] => http://localhost/test/wp-content/uploads/2015/06/Wallpaper_55-212x159.jpg
        )

    [2] => Array
        (
            [wpc_resize_thumb_img] => http://localhost/test/wp-content/uploads/2015/06/Wallpaper_51-212x141.jpg
        )
)

缩略图

$wpc_resize_thumb_images = get_post_meta($post->ID, 'wpc_resize_thumb_images', true);
echo "<pre>";
    print_r($wpc_resize_thumb_images);
echo "</pre>;

结果是

Array
(
    [1] => Array
        (
            [wpc_resize_big_img] => http://localhost/test/wp-content/uploads/2015/06/Wallpaper_55-500x375.jpg
        )

    [2] => Array
        (
            [wpc_resize_big_img] => http://localhost/test/wp-content/uploads/2015/06/Wallpaper_51-500x333.jpg
        )
)

现在我的问题是如何使用一个元键合并并保存在数据库中,当我打印元键时它会给我这样的结果

我要这个

Array
(
    [1] => Array
        (
            [wpc_resize_thumb_img] => http://localhost/test/wp-content/uploads/2015/06/Wallpaper_55-212x159.jpg
            [wpc_resize_big_img] => http://localhost/test/wp-content/uploads/2015/06/Wallpaper_55-500x375.jpg
        )

    [2] => Array
        (
            [wpc_resize_thumb_img] => http://localhost/test/wp-content/uploads/2015/06/Wallpaper_51-212x141.jpg
            [wpc_resize_big_img] => http://localhost/test/wp-content/uploads/2015/06/Wallpaper_51-500x333.jpg
        )
)

保存多维数组时可以使用此代码:

$product_img_path[$count]['wpc_resize_thumb_img'] = $upload_dir['url'].'/'.$resize_img_name;
$product_img_path[$count]['wpc_resize_bid_img'] = $upload_dir['url'].'/'.$resize_big_img_name;
update_post_meta($post->ID, 'wpc_images', $product_img_path);

这样你就可以得到你想要的多维数组:

Array
(
    [1] => Array
        (
            [wpc_resize_thumb_img] => http://localhost/test/wp-content/uploads/2015/06/Wallpaper_55-212x159.jpg
            [wpc_resize_big_img] => http://localhost/test/wp-content/uploads/2015/06/Wallpaper_55-500x375.jpg
        )

[2] => Array
    (
        [wpc_resize_thumb_img] => http://localhost/test/wp-content/uploads/2015/06/Wallpaper_51-212x141.jpg
        [wpc_resize_big_img] => http://localhost/test/wp-content/uploads/2015/06/Wallpaper_51-500x333.jpg
    )
)