Posted By

Piotrek290 on 04/08/10


Tagged

php object array json convert


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

umang_nine
dantreacy
romainsauger


From PHP Array to JSON


 / Published in: PHP
 

  1. <?php
  2. /*
  3. Autor: Piotr Filipek
  4. Strona: www.dealis.pl
  5. Email: [email protected]
  6.  
  7. Quick making JSON Array with objects :)
  8. */
  9.  
  10. class JSONResponse {
  11. public $response = array();
  12.  
  13. public function add($arr){
  14. $par = array();
  15. foreach($arr as $item => $value){
  16. $ret2 = "'".$item."' : '".$value."'";
  17. array_push($par, $ret2);
  18. }
  19.  
  20. $par = join(",", $par);
  21.  
  22. array_push($this->response, "{".$par."}");
  23. }
  24.  
  25. public function getResponse(){
  26. return "[".join(",", $this->response)."]";
  27. }
  28. }
  29. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: s0l1dsnak3123 on April 13, 2010

Why can't you just use json_encode($array,TRUE); ?

You need to login to post a comment.