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.  
  6. Quick making JSON Array with objects :)
  7. */
  8.  
  9. class JSONResponse {
  10. public $response = array();
  11.  
  12. public function add($arr){
  13. $par = array();
  14. foreach($arr as $item => $value){
  15. $ret2 = "'".$item."' : '".$value."'";
  16. array_push($par, $ret2);
  17. }
  18.  
  19. $par = join(",", $par);
  20.  
  21. array_push($this->response, "{".$par."}");
  22. }
  23.  
  24. public function getResponse(){
  25. return "[".join(",", $this->response)."]";
  26. }
  27. }
  28. ?>

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.