是否有PHP函数来编码/解码字符串中的多维数组?

问题描述:

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.

是否有PHP函数将通用多维数组编码为字符串并将其作为多维数组返回? / p>

我需要它在mysql中存储一些数据(精确的drupal计算字段)。 该数组只包含浮点数和字符串。 p> div>

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.