Skip to content
Advertisement

Php function serialize() returns s:value but i don’t want this ‘s:’.What should I do?

I got this as an O/P:

s:287:”a:3:{s:6:”actors”;a:2:{s:4:”name”;s:6:”Actors”;s:5:”value”;s:38:”Felicity Jones, Diego Luna, Alan Tudyk”;}s:8:”director”;a:2:{s:4:”name”;s:8:”Director”;s:5:”value”;s:14:”Gareth Edwards”;}s:6:”writer”;a:2:{s:4:”name”;s:6:”Writer”;s:5:”value”;s:36:”Chris Weitz, Tony Gilroy, John Knoll”;}}”;

My Code:-

JavaScript

I don’t need first s:287″ but this thing automatically save in my database. Help me please..

Advertisement

Answer

The WordPress function add_post_meta() tries to serialize the passed data for itself.

JavaScript

As you can see in the documentation of add_post_meta() it calls internally the add_metadata() function. This function checks for data, that has to be serialized by calling maybe_serialize(). For BC reasons it doubly serializes already serialzed strings as mentioned in the comments of the function.

Conclusion: Just pass the unserialzed array to the add_post_meta() function.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement