Question

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

Was it helpful?

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)).

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top