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 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿