dongzouche9108 2013-10-09 01:55
浏览 52
已采纳

递归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"
  • 写回答

1条回答 默认 最新

  • dongzhi8984 2013-10-09 02:47
    关注

    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"
        }
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么