Question

I ask serialization and unserialization by PHP script. Only sometimes I edit the array manually (in database.txt).

Était-ce utile?

La solution

You could export the array as a JSON (JavaScript Object Notation) string and later decode it again.

You can do this with the functions json_encode and json_decode, respectively.

$a = array(
  'a' => 1,
  'b' => 2,
  'c' => array(1,2,3));
file_put_contents('database.txt', json_encode($a));
// encoded: {a:1,b:2,c:[1,2,3]}

// ---
$a = json_decode(file_get_contents('database.txt'), TRUE);
// specify TRUE to parse objects as assoc. arrays ---^

Note, that all this might not be the best idea (I'm a bit worried about the filename you chose – if you want to have a DB, then use a DB! (e.g. sqlite)).

Autres conseils

You should take a look at the JSON format. Depending on what type of data you are serializing, this should do what you want, and it's pretty human readable and easily edited.

In particular, your array is limited to containing only numbers, strings, floats, boolean values, or other arrays. However, you can store both numerically indexed as well as associative arrays.

For more information on the standard, take a look at http://www.json.org/ and for the php functions, see http://us2.php.net/json_encode and http://us2.php.net/json_decode

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top