简体   繁体   中英

Is there a PHP function to encode/decode a multidimensional array in a string?

Is there a PHP function to encode a generic multidimensional array as a string and get it back as a multidimensional array?

I need it to store in mysql some data (a drupal computed field to be precise). The array contains just floats and strings.

serialize() and unserialize() do what you describe.

http://www.php.net/manual/en/function.serialize.php

http://www.php.net/manual/en/function.unserialize.php

You could also consider encoding the array as JSON with json_encode() and json_decode() , which gives more readable output, if that is important to you.

I second the use of "json_encode" and "json_decode". I believe the output of "json_encode" is less verbose than PHP's serialize function (since data types are inferred) and is immediately more portable (even though that is not a requirement).

Make sure you pass "TRUE" for the second parameter of "json_decode", otherwise you may get a simple object back, depending on how the original data was encoded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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