简体   繁体   English

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

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

I am working on WordPress upload meta fields. 我正在WordPress上载meta字段。 When user upload images the images are sized in two dimension one is "thumb" and one is "big" and they re sized very perfectly. 当用户上传图像时,图像的尺寸是二维的,一是“拇指”,一是“大”,它们的尺寸调整得非常完美。 I save both of images dimension path in database with different meta keys Like: 我使用不同的元键将两个图像尺寸路径保存在数据库中,例如:

for thumb image wpc_resize_thumb_images and for big images wpc_resize_big_images . 用于拇指图像wpc_resize_thumb_images和用于大图像wpc_resize_big_images

When i save images path in DB it save perfectly. 当我在DB中保存图像路径时,它可以完美保存。

Here is my code to save them in 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);

In Database it save like this: 在数据库中保存如下:

meta_key meta_key

wpc_resize_big_images wpc_resize_big_images

meta_value 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);

In Database it save like this: 在数据库中保存如下:

meta_key meta_key

wpc_resize_thumb_images wpc_resize_thumb_images

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

And when i print them they show me result like this: 当我打印它们时,它们向我显示如下结果:

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

and result is 结果是

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

and result is 结果是

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

Now My question that how can i merge and save in database both dimension with one meta key and when i print the meta key it gives me result like this 现在我的问题是,我怎样才能用一个元键合并和保存维度,当我打印元键时,它会给我这样的结果

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

When you are saving the multidimensional array you can use this code: 保存多维数组时,可以使用以下代码:

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

That way you can get multidimentional array like you want: 这样,您可以根据需要获取多维数组:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM