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などのデータも連想配列で返ってくるのでこの関数とデータベースを組み合わせると、よりよくわからない物ができそうです。

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

73 件のコメント:

  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

    返信削除
  28. Wow! Thank you! I continuously needed to write on my site something like that. Can I include a part of your post to my site?
    사설토토

    返信削除
  29. Fine way of explaining, and nice piece of writing to get facts concerning my presentation focus, which i am going to deliver in institution of higher education. 카지노

    返信削除
  30. Keep up the good work , I read few blog posts on this website and I believe that your weblog is rattling interesting. Thank you for this effort, I will give you 5 stars for this. Kindly check the link below Thank you! 토토

    返信削除
  31. I was able to think a lot while looking at this post.
    I will pass this blog to many people. Definitely a good blog is very good. Thank you for sharing. 온라인카지노

    返信削除
  32. Thanks for sharing not only the good, but the hard and the bad as well. People look one way from the outside: accomplished, confident, whatever. It’s a gift that you share the fears and insecurities with your readers too. 파워볼게임

    返信削除
  33. Took me time to read all the comments, but I genuinely enjoyed the article. It proved to become extremely beneficial to me and I am positive to all of the commenters here! 카지노

    返信削除
  34. Woah! I'm really enjoying the template/theme of this blog.

    It's simple, yet effective. A lot of times it's difficult to get
    that "perfect balance" between superb usability and appearance.
    I must say that you've done a superb job with this. Additionally,
    the blog loads extremely fast for me on Opera. Excellent Blog!
    온라인카지노

    返信削除
  35. Wonderful beat ! I wish to apprentice while you amend
    your web site, how can i subscribe for a blog site? The account helped me a acceptable deal.
    I had been a little bit acquainted of this your broadcast
    provided bright clear idea. 경마

    返信削除
  36. I think this is among the most important information for me.
    And i am glad reading your article. But should remark on few general things,
    The website style is ideal, the articles is really nice : D.
    Good job, cheers


    https://www.totositehot.com

    返信削除
  37. Hello there! Quick question that’s completely off topic.
    Do you know how to make your site mobile friendly? My website looks weird when viewing from my iphone.
    I’m trying to find a template or plugin that might
    be able to resolve this issue. If you have any recommendations, please share.
    Thank you!

    website:스포츠토토

    返信削除
  38. Proceeded with instructive choices will efficiently, such as white stable light co. Great on sign in should get into the worker entrance will give of installments. Multi month following the left portion of the key phrase. As soon as you can permit you need to share your company related to love during the accreditation. Entryway establishment direct you will need to land their standing chances to your 야한소설

    返信削除
  39. I can’t imagine focusing long enough to research; much less write this kind of article. You’ve outdone yourself with this material. This is great content.대딸방

    返信削除
  40. Excellent post but I was wanting to know if you could write a little more on this subject? I’d be very grateful if you could elaborate a little bit more. Many thanks!

    스포츠마사지

    返信削除
  41. We’re a group of volunteers and opening a new scheme in our community. Your web site provided us with valuable info to work on. You’ve done an impressive job and our entire community will be thankful to you. 출장마사지

    返信削除
  42. ¡Gracias por tu maravillosa publicación personal!
    Disfruté bastante leyéndolo, resulta que eres un gran autor.

    Me aseguraré de marcar tu blog como favorito y, finalmente, volveré más tarde en la vida.
    Quiero animarte a continuar con tu gran trabajo, tener una
    buenos dias!


    https://www.totosafeguide.com

    返信削除
  43. Good post but I was wondering if you could write a litte more on this subject? I’d be very thankful if you could elaborate a little bit further. Appreciate it! 토토사이트

    返信削除
  44. Уour blog providеd us useful information to work on. Үou have done a marvelous job! 슬롯머신사이트

    返信削除
  45. To activate retail card first click mcafee.com/activate to redeem your product key. Enter the unique activation code found on your Retail Card or eCard. Follow our onscreen steps to download and install your security app.
    cash app login |
    mcafee.com/activate |
    mcafee.com/activate

    返信削除
  46. Ce poste est très utile et très instructif, il n'y a aucun doute à ce sujet. J'ai trouvé celui-ci assez fascinant et il devrait faire partie de ma collection. Très bon travail!


    https://www.safecasinosite.net

    返信削除
  47. GTA 5 - Beating The 토토사이트 Heist (The Big Con) $2,200,000

    返信削除
  48. I needed to thank you for this great read!! I definitely enjoyed every little bit of it. I’ve got you book-marked to look at new stuff you post? Feel free to visit my website; 카지노사이트

    返信削除
  49. It's late finding this act. At least, it's a thing to be familiar with that there are such events exist. I agree with your Blog and I will be back to inspect it more in the future so please keep up your act. Feel free to visit my website; 토토사이트

    返信削除
  50. I found your blog while I was webs writing to find the above information. Your writing has helped me a lot. I'll write a nice post by quoting your post. Feel free to visit my website; 카지노사이트

    返信削除
  51. Greetings! Very helpful advice within this post! It's the little changes that will make the most important changes. Thanks for sharing! Feel free to visit my website; 카지노사이트

    返信削除
  52. Hey man, .This was an excellent page for such a hard subject to talk about. I look forward to reading many more great posts like these. Thanks Feel free to visit my website; 배트맨토토

    返信削除
  53. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Great work. 토토사이트

    返信削除
  54. Thank you very much for this one. And i hope this will be useful for many people. 파워볼

    返信削除
  55. I would appreciate you that you pick up an important topic to write a thoroughly informative post on. I hope that you never stop and keep posting such valuable content 스포츠토토

    返信削除
  56. The best way to avoid paying stamp duty on a second home is to purchase it as a joint ownership. If you're married or in a civil partnership, you can avoid paying additional stamp duty if you're a first-time homebuyer. However, this option is not available for second homes, as the rule only applies to main residences. In this case, you can try to negotiate with the developer for a stamp duty waiver.

    how to avoid stamp duty on second home

    返信削除
  57. When preparing a tax return for a client, it can be beneficial to know how to calculate marginal taxes. The rates in different countries vary, but most of them are based on the income range. The higher your tax bracket, the higher your tax rate. In general, if you're in the highest tax bracket, you'll have to pay the same amount of taxes as someone in the lowest tax bracket. Here is an example of how to calculate marginal tax rates.

    how to calculate marginal tax rate

    返信削除
  58. If you've ever wondered why your house is so dusty, you're not alone. The particles of dust are made up of dead skin cells, insects, clothing fibers, and even hair. When there is too much dust in a room, you've got a problem. There are 6 common reasons for your house to be so dusty. Find out what they are, and how you can solve them.

    why is my house so dusty

    返信削除
  59. It is possible for foreigners to purchase property in the UK. Although the process is complicated, it is possible for non-UK residents to own property. However, it is important to bear in mind that there are certain conditions that must be met before foreigners can buy property in the UK. For example, it may be difficult to qualify for a mortgage if they do not work in the UK. Furthermore, if they do not have two years of residency in the UK, they will have to pay more for a larger deposit.

    can foreigners buy property in the uk

    返信削除
  60. The Manchester Property Market is booming, and for good reason. The city is one of the fastest growing in the UK, and the average rental yield in Central Manchester and Salford is higher than in most of the country. More than 60% of the local population rent their homes, which means that the demand for new housing is constantly increasing. The demand for homes is a reflection of the city's growing population and improving economy. With the demand coming from both existing residents and newcomers, it makes sense for property investors to purchase properties in this area. Manchester Property Market

    返信削除
  61. Are you wondering: "Why Are There So Many Flies in My House?" The answer to this question is simple: they're summer bugs. While they can be annoying, they're also harmless. You can't kill flies, but you can kill them by vacuuming them up. If you've noticed too many flies in your home, consider these tips to keep them away.

    Why Are There So Many Flies in My House

    返信削除
  62. If you're in the market for a new home, you may wonder how long does a mortgage pre approval last. Generally, it takes 90 to 120 days to be approved, but you may qualify for a longer or shorter period of time. If you have strong credit, your preapproval will typically last three to four months. However, if your financial situation has changed significantly, your preapproval may last up to two years. how long does a mortgage pre approval last

    返信削除

  63. Appreciate it for helping out, excellent info
    토토사이트

    返信削除


  64. Nice post!I have never seen such a brilliantly written article in a long time. I am thankful to you that you produced this!
    바카라사이트

    返信削除

  65. Nicee post!!Thanks for sharing this quality information with us. I really enjoyed reading.
    토토사이트

    返信削除

  66. I am constantly surprised by the amount of information accessible on this subject.
    토토사이트

    返信削除
  67. This post is very simple to read and appreciate without leaving any details out. Great work!
    Okbet Online Gambling 2022

    返信削除