JSON - JSON กับ PHP

Post a reply


In an effort to prevent automatic submissions, we require that you complete the following challenge.
Smilies
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
View more smilies

BBCode is ON
[img] is ON
[flash] is ON
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: JSON - JSON กับ PHP

JSON - JSON กับ PHP

by buay » 10/06/2014 6:25 pm

แปลงโค้ดจาก PHP เป็น JSON

File : encode.php

Code: Select all

<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
echo json_encode($arr);
?>
ผลที่ได้
05.png
05.png (7.3 KiB) Viewed 821 times
ตัวอย่างการแปลง PHP Object เป็น JSON

File : phpobj.php

Code: Select all

<?php
   class Ex { 
	   public $id = "";
       public $Firstname = "";
       public $Lastname  = "";      
	   public $Date = "";
   }
   $aa = new Ex();
   $aa->id = "999";
   $aa->Firstname = "mindphp";
   $aa->Lastname  = "com";   
   $aa->Date = date('m-d-Y , h:i:s a', strtotime('now'));
   
   echo json_encode($aa);
?>
ผลที่ได้
07.png
07.png (8.97 KiB) Viewed 821 times
แปลงโค้ดจาก JSON เป็น PHP

ตัวอย่าง File : decode.php

Code: Select all

<?php
   $json = '{"1":"ab","2":"cde","3":"fghi","4":"jklmn"}';
   var_dump(json_decode($json));
   echo "<br>";
   var_dump(json_decode($json, true));
   
?>
ผลที่ได้
06.png
06.png (12.1 KiB) Viewed 820 times

Top