Jump to content

Coding a nested serialized PHP array


Go to solution Solved by Barand,

Recommended Posts

Hi,

 

I am creating an import using WP All Import in Wordpress and I've been told by their support that I have to write some custom code to create a nested serialised PHP array. 

 

The serialised array has to end up looking like this:

 

s:287:"a:8:{s:5:"price";a:2:{s:5:"value";s:5:"38000";s:8:"original";s:0:"";}s:17:"custom_tax_inside";s:0:"";s:15:"custom_tax_page";s:0:"";s:8:"city_mpg";a:1:{s:5:"value";s:0:"";}s:11:"highway_mpg";a:1:{s:5:"value";s:0:"";}s:12:"custom_badge";s:0:"";s:5:"video";s:0:"";s:10:"short_desc";s:0:"";}";

 

Can you give me any advice on how I can:

a) See what this nested serialised array actually looks like

b) How I can code a nested serialised array like this

 

Once I can do this I plan to put it into a hook for when WP All Import is importing and this will allow me to import certain values (like price) into my car database.

 

Any input from anyone in the community would be much appreciated.

 

Bonus Point: I have another field that containst he following serialised array:

 

a:7:{i:0;s:38:"Multi-function Control Screen - Colour";i:1;s:17:"11 Speaker Stereo";i:2;s:15:"Online Services";i:3;s:19:"18inch Alloy Wheels";i:4;s:21:"Power - Tailgate/Boot";i:5;s:45:"Power Door Mirrors - Auto Dipping (Reversing)";i:6;s:13:"Driving Lamps";}

 

When I put this into 'http://www.unserialize.com/' I get the following:

 

'Array

(
[0] => Multi-function Control Screen - Colour
[1] => 11 Speaker Stereo
[2] => Online Services
[3] => 18inch Alloy Wheels
[4] => Power - Tailgate/Boot
[5] => Power Door Mirrors - Auto Dipping (Reversing)
[6] => Driving Lamps
)'

 

However when I type in the following:

 

s:287:"a:8:{s:5:"price";a:2:{s:5:"value";s:5:"38000";s:8:"original";s:0:"";}s:17:"custom_tax_inside";s:0:"";s:15:"custom_tax_page";s:0:"";s:8:"city_mpg";a:1:{s:5:"value";s:0:"";}s:11:"highway_mpg";a:1:{s:5:"value";s:0:"";}s:12:"custom_badge";s:0:"";s:5:"video";s:0:"";s:10:"short_desc";s:0:"";}";

 

I get the following:

 

s:287:"a:8:{s:5:"price";a:2:{s:5:"value";s:5:"38000";s:8:"original";s:0:"";}s:17:"custom_tax_inside";s:0:"";s:15:"custom_tax_page";s:0:"";s:8:"city_mpg";a:1:{s:5:"value";s:0:"";}s:11:"highway_mpg";a:1:{s:5:"value";s:0:"";}s:12:"custom_badge";s:0:"";s:5:"video";s:0:"";s:10:"short_desc";s:0:"";}";

 

I just get a string with no array values - what am I doing wrong? Is there something wrong with the serialised array above?

Link to comment
https://forums.phpfreaks.com/topic/303179-coding-a-nested-serialized-php-array/
Share on other sites

  • Solution

Yes - it has been serialized twice.

Serialize the array into a string

Serialize the resulting string

 

To unravel it, unserialize twice

print_r( unserialize(unserialize('s:287:"a:8:{s:5:"price";a:2:{s:5:"value";s:5:"38000";s:8:"original";s:0:"";}s:17:"custom_tax_inside";s:0:"";s:15:"custom_tax_page";s:0:"";s:8:"city_mpg";a:1:{s:5:"value";s:0:"";}s:11:"highway_mpg";a:1:{s:5:"value";s:0:"";}s:12:"custom_badge";s:0:"";s:5:"video";s:0:"";s:10:"short_desc";s:0:"";}"')));

Gives

Array
(
    [price] => Array
        (
            [value] => 38000
            [original] => 
        )

    [custom_tax_inside] => 
    [custom_tax_page] => 
    [city_mpg] => Array
        (
            [value] => 
        )

    [highway_mpg] => Array
        (
            [value] => 
        )

    [custom_badge] => 
    [video] => 
    [short_desc] => 
)
  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.