2012年12月24日月曜日

僕の彼女を紹介しつつPHPでJSONファイルを扱います!

B13の笹本(偽名)こと大矢です。
今日はアレですね!アレ!という事で今日は僕の彼女を紹介します
こちらが彼女の写真です。ウェブサイトも持っているようです。見てあげてください。
紹介はこのくらいにします。本日はどうも、ありがとうございました。

などとここで終わらせる勇気が無かったのでPHPでのJSONファイルの扱い方について紹介しようと思います。こちらの記事をかなーり参考にしてPHPで使えるJSONクラスを作ってみました。

機能としてはJSONファイルを連想配列として読み込み、追加の処理を行い、また連想配列をJSON文字列として変換し保存する事ができます。
動作としてはコンストラクタの引数から得たファイル名でJSONデータを読み込み、addメソッドでデータを追加し、saveメソッドでJSONデータをファイルとして保存、closeメソッドでファイルを閉じています。ソースコードは以下のような感じです。

class Json {
 private $data;
 private $fp;
 private $filename;
 private $path = "./";

 public function __construct($name){

  $this->filename = $this->path . $name;
  $this->fp = fopen($this->filename,"r+");
  
  if(file_exists($this->filename)){
   flock($this->fp,LOCK_EX);
   $this->data = json_decode(fread($this->fp, filesize($this->filename)));

  }else{
   $this->data = array();
  }
 }

 public function add($data){
  array_push($this->data,$data);
 }

 public function save(){
  $this->fp = fopen($this->filename,"wb");
  fwrite($this->fp,json_encode($this->data));
 }

 public function close(){
  flock($this->fp, LOCK_UN);
  fclose($this->fp);
 }

}

このクラスを用いると以下のような感じになります。


$json = new Json("test.json");

$post_data = array("name" => "笹本","text" => "テストだよ");

$json->add($post_data);

$json->save();
$json->close();

上記の処理でname属性の『笹本』、test属性の『テストだよ』という文字列が『test.json』ファイルに追加されています。これをPOSTのデータ等が追加されるように変更していけば色々な物が作れそうです。

PHPでJsonを扱うには『json_decode』関数と『json_encode』関数を使用しています。 『json_decode』はJSON文字列を連想配列データとして変換。
『json_encode』は連想配列データをJSON文字列に変換してくれます。
MySQLなどのデータも連想配列で返ってくるのでこの関数とデータベースを組み合わせると、よりよくわからない物ができそうです。

ちなみに坂井くんの彼女はこの子らしいです、誰かが言っていました。怒られたら消します。ありがとうございました。

28 件のコメント:

  1. Mohon ijin berpromosi. service Ac segala jenis merk. kami service Ac mencakup wilayah coverage seluruh Surabaya, Gresik, Sidoarjo. kami juga menerima jasa service perbaikan AC bongkar dan pasang AC. pekerjaan kami cepat, tepat, profesional, harga kompetitif di banding harga jasa luaran.
    service ac split sidoarjo
    tukang service ac sidoarjo
    service ac surabaya sidoarjo
    service ac surabaya selatan
    service ac surabaya timur
    service ac surabaya barat
    service ac surabaya utara
    service ac kota surabaya

    SPLIT-TYPE AIR CONDITIONERS
    MULTI-SPLIT AIR CONDITIONERS
    CHILLED WATER FAN COIL UNITS
    VRF AIR CONDITIONERS
    CHILLERS
    WINDOW-TYPE AIR CONDITIONERS

    http://jualjasaacsurabaya.blogspot.com
    Segera hubungi kami, dan kami akan datang kerumah Anda 24 Jam Non Stop.
    (Hari Sabtu / Minggu / Hari Libur buka)
    PIN BBM : 54BE0B4B / 5474CD93 / TELP : 085645475574

    返信削除
  2. Are you looking for the best smartwatch in india, then here is the list of the best smartwatch in india under 5000 which help you to choose best smartwatch india.

    返信削除
  3. It’s always interesting to read articles from other authors and practice
    something from their web sites. 일본야동

    返信削除
  4. Wonderful article! That is the kind of information that are
    meant to be shared around the net. Disgrace on the seek engines for not positioning this publish upper!
    Come on over and talk over with my site . Thank you =) 일본야동

    返信削除
  5. Ӏf ʏou are іnterested, feel free to ѕend սѕ your articles foг օur rdview at:
    국산야동

    返信削除
  6. I am interested in this area if you are a great site but know some community forums. I think it should be a good opportunity to get feedback from others who are experienced. 일본야동

    返信削除
  7. Hi! I just would like to give you a huge thumbs up for the great info you have got right here on this post. I'll be coming back to your site for more soon. 국산야동

    返信削除
  8. We are really grateful for your blog post for giving a lot of information
    카지노사이트

    返信削除
  9. It's difficult to find well-informed people about this topic, but you seem like you know what you're talking 바카라사이트about! Thanks

    返信削除
  10. This article is very helpful and interesting too. 토토사이트 Keep doing this in future. I will support you.

    返信削除
  11. This is really helpful post and very informative there is no doubt about it.
    카지노사이트

    返信削除
  12. Wonderful issues altogether, you just received a brand new reader.
    What may you recommend in regards to your post that you just made
    some days in the past? Any sure? 카지노


    返信削除
  13. Very nice post. I just stumbled upon your blog and wished to say that
    I have really enjoyed browsing your blog posts. In any case I'll be subscribing to your
    feed and I hope you write again very soon!오피

    返信削除
  14. Having read this I believed it was extremely informative. I appreciate you finding the time and energy to put this content together. 파워볼

    返信削除
  15. Wonderful website. Plenty of helpful information here. 경마사이트

    返信削除
  16. Real money online casinos are safe and secure to play at and your most important choice is going to be looking for great game selection 릴게임

    返信削除
  17. Online casinos are packed with all the games you’ll find in any land casino. 토토사이트

    返信削除
  18. What’s up to every , for the reason that I am really keen of reading this blog’s post to
    be updated daily. It consists of fastidious material.온라인바둑이

    返信削除
  19. It’s so good and so awesome. I am just amazed. I hope that you continue to do your work.바카라사이트

    返信削除
  20. I have a lot of spray lining knowledge but always learn something new. Keep up the good work and thank you again.카지노사이트탑

    返信削除
  21. Your site is excellent. I was really touched by this short article. I'm so pleased to discover the post I have actually been searching for a very long time. I covet the amount of individuals sympathize with your writing. You discover a great deal of understanding from my writing.바카라사이트

    返信削除
  22. What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. Thank you very much.
    일본경마

    magosucowep

    返信削除
  23. Howdy! I simply want to give you a big thumbs up for the excellent info you’ve got right here on this post 토토

    返信削除

  24. Thank you a bunch for sharing this with all of us you actually realize what you are talking about! Bookmarked.
    Please also seek advice from my site =). We could have a hyperlink change contract between u 오피월드




    返信削除
  25. I have learn this put up and if I may just I desire to suggest you some attention-grabbing issues or tips.
    파워볼

    返信削除
  26. i was looking it for so long , and i found it finally that i know it will help me .
    온라인카지노사이트

    返信削除
  27. I have learn this put up and if I may just I desire to suggest you some attention-grabbing issues or tips.
    스포츠토토365

    返信削除