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

时间:2015-06-18 作者:deemi-D-nadeem

我正在处理WordPress上传元字段。当用户上传图像时,图像的大小是二维的,一个是“拇指”,另一个是“大”,它们的大小调整得非常完美。我使用不同的元键在数据库中保存两个图像维度路径,如:

对于拇指图像wpc_resize_thumb_images 对于大图像wpc_resize_big_images.

当我将图像路径保存在DB中时,它可以完美地保存。

以下是我在DB中保存它们的代码:

For 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\\u resize\\u big\\u图像

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";}}

and for thumb images

$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\\u resize\\u thumb\\u图像

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";}}
当我打印它们时,它们会显示如下结果:

Big Imaegs

$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
        )
)

Thumb Images

$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
        )
)
现在我的问题是,如何用一个元键将两个维度合并并保存到数据库中,当我打印元键时,它会给出如下结果

I want this

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
        )
)
如果你理解我期望的结果,请给出答案,并请给我测试的答案。请紧急给我答复。我希望你能理解我的问题

1 个回复
最合适的回答,由SO网友:Omer 整理而成

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

$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
    )
)

结束