Jump to content


Photo

Coding a nested serialized PHP array


Best Answer Barand, 13 February 2017 - 10:54 AM

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] => 
)
Go to the full post


  • Please log in to reply
2 replies to this topic

#1 kostakondras

kostakondras
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 13 February 2017 - 08:26 AM

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?



#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 17,594 posts

Posted 13 February 2017 - 10:54 AM   Best Answer

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

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 kostakondras

kostakondras
  • Members
  • PipPip
  • Member
  • 20 posts

Posted 13 February 2017 - 10:57 PM

Aight - thanks. I'll try and do the import again - this has helped a lot. 






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users