MrPolywhirl

CycleJson.php

Oct 8th, 2013
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.78 KB | None | 0 0
  1. $json = '{"foo":"bar",
  2.          "order":["first", "second", "third"],
  3.          "pairs":[{"id":"0"}, {"id":"1"}]}';
  4.  
  5. function doObject($__obj){      
  6.     $__obj = cycleObject($__obj);      
  7.     return $__obj;  
  8. }
  9.  
  10. function cycleObject($__obj){  
  11.     foreach($__obj as $key => &$val){  
  12.         if(is_object($val)) {
  13.             cycleObject($val);
  14.         }
  15.         if(is_array($val)) {
  16.             foreach($val as &$v) {
  17.                 if(is_object($v) || is_array($v)) {
  18.                     cycleObject($v);
  19.                 } else {
  20.                     $v .= '-ok';
  21.                 }
  22.             }
  23.         }
  24.         if(is_string($val)) {
  25.             $val .= '-ok';
  26.         }
  27.     }  
  28.     return $__obj;
  29. }
  30.  
  31. $obj = doObject(json_decode($json));
  32. var_dump($obj);
Advertisement
Add Comment
Please, Sign In to add comment