递归JSON转换

Suppose I have a JSON string:

$json = '{"lemon":"test",
          "orange":["toto", "tata", "tete"],
          "zob":[{"id":"0"}, {"id":"1"}]}';

I'd like to cycle through that encoded object to modify every string in it, so I have a recursive function:

function doObject($__obj){
  $__obj = cycleObject($__obj);
  return $__obj;
}

function cycleObject($__obj){
  $type = gettype($__obj);
  foreach($__obj as $var => &$val){
    switch(gettype($val)){
      case 'object':
      cycleObject($val);
      break;

      case 'array':
      cycleObject($val);
      break;

      case 'string':
      if($type == 'object'){
        $__obj->$var = $val.'-ok';
      }else{
        if($type == 'array'){
          $__obj[$var] = $val.'-ok';
        }
      }
      break;
    }
  }
  return $__obj;
}

And I call the function:

$obj = doObject(json_decode($json));
var_dump($obj);

Which gives :

object(stdClass)#1 (3) {
    ["lemon"]=> string(7) "test-ok"
    ["orange"]=> array(3) {
        [0]=> string(4) "toto"
        [1]=> string(4) "tata"
        [2]=> string(4) "tete" }
    ["zob"]=> array(2) {
        [0]=> object(stdClass)#2 (1) {
            ["id"]=> string(4) "0-ok" }
        [1]=> object(stdClass)#3 (1) {
            ["id"]=> string(4) "1-ok" }
    }
}

Now my problem is, for some reason, I am unable to modify directly inside an array composed by string, or should I say, the modified string inside an array (and not inside an object inside an array) because the array loses its reference. How do I fix that so in orange I instead obtain:

[0]=> string(7) "toto-ok"
[1]=> string(7) "tata-ok"
[2]=> string(7) "tete-ok"

Your array of strings isn't being scrutinized correctly by your function. Basically, in each array you need a second check to see if you are dealing with another array/object or a string, otherwise regular arrays of strings are being bypassed....oddly enough. The following should work for you:

$json = '{"lemon":"test", 
          "orange":["toto", "tata", "tete"], 
          "zob":[{"id":"0"}, {"id":"1"}]}';

function doObject($__obj){      
    $__obj = cycleObject($__obj);       
    return $__obj;  
}

function cycleObject($__obj){   
    foreach($__obj as $key => &$val){  
        if(is_object($val)) {
            cycleObject($val);
        }
        if(is_array($val)) {
            foreach($val as &$v) {
                if(is_object($v) || is_array($v)) {
                    cycleObject($v);
                } else {
                    $v .= '-ok';
                }
            }
        }
        if(is_string($val)) {
            $val .= '-ok';
        }
    }   
    return $__obj;
}

$obj = doObject(json_decode($json));
var_dump($obj);

This produced the results you were looking for in my local environment.

object(stdClass)#1 (3) {
  ["lemon"]=>
  string(7) "test-ok"
  ["orange"]=>
  array(3) {
    [0]=>
    string(7) "toto-ok"
    [1]=>
    string(7) "tata-ok"
    [2]=>
    string(7) "tete-ok"
  }
  ["zob"]=>
  array(2) {
    [0]=>
    object(stdClass)#2 (1) {
      ["id"]=>
      string(4) "0-ok"
    }
    [1]=>
    object(stdClass)#3 (1) {
      ["id"]=>
      string(4) "1-ok"
    }
  }
}