开发者

php how to write data into json?

I am newer for php. I want make php page cache, query data from mysql and store data into json format.

I have many questions:

  1. which type of file should I store? .json or .txt or .cache? for I also need use json decode return datas into page.

  2. I want use cron tab, make many mysql queries and write开发者_如何转开发 into one json file. what write code should I choose? fopen, fwrite or file_get_contents or other command? (do not cover the data, but continue write. I will deleted the file and renewer it at the next cron time)

  3. If a multi write into a json data (10 or more mysql query at the same time and write into a same json file, each json child format like {name: ".$row['name']."}), how to completed a top { and bottom } to make a standad json data format?

    { //how to add this one

    {name: ".$row['name']."}

    {name: ".$row['name']."}

    // many name from 10 more mysql queries

    } //and this one

Thanks.


It's json_encode()

json_encode() — Returns the JSON representation of a value

<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

echo json_encode($arr);
?>


which type of file should I store

It doesn't matter. There is no fixed extension, but I would pick .json just to make it clear what the file is supposed to contain.

what write code should I choose?

Just use file_put_contents to put the JSON string (see next section) into a file.

each json child format like

You really do not want to use that method. It might work for a while, but becomes very complex when you need to handle things like quoting and special-character escapes. Instead of re-inventing the wheel, use PHP's built-in JSON functions for this.

Create the data-structure you want using PHP's strings, numbers, and arrays, and then rely on json_encode to turn it into a string.

The main thing to be careful of is that depending on how your php array() looks, you might get JSON [] versus {}.


  1. As far as saving the file as .txt or .json won't make a difference.

  2. I think the focal point of this all lies in the json_encode page. Here's the example from that page:

This code:

<?php
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);

echo json_encode($arr);
?>

Outputs like this:

{"a":1,"b":2,"c":3,"d":4,"e":5}

3 . You can use fopen and fwrite to write to your file. The second argument to fopen is the mode, you want to use 'a' for append.


Don't write your own cache because anything you write in PHP will be slower than can be supported by native extensions (like APC or memcached or even MySQL itself!!).

Don't cache as JSON. JSON is not a particulary 'fast' to serialize. If you're doing caching you don't want to do any serialization at all. Just store it as it is.

MySQL does query caching for you. If performance is a problem first tune your MySQL queries and database schema. Caching is one of the absolute last optimization you want to do.

If you want an easy way to cache, make a MySQL table called 'cache' and use that. If you want quick (small) file access, use MySQL (seriously). If you want an even faster cache access use an in-memory cache like APC or memcached.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜