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

223 views Asked by At

I am working on WordPress upload meta fields. 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.

When i save images path in DB it save perfectly.

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

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

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

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

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

There are 1 answers

0
Omer On BEST ANSWER

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