문제

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

도움이 되었습니까?

해결책

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

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top