【问题标题】:Need to insert new key value pairs recursiverly inside every children json array需要在每个子 json 数组中递归地插入新的键值对
【发布时间】:2021-08-29 00:23:47
【问题描述】:

我有一个嵌套的 json 对象数组:

$json_url_data = [{
    "form_name": "z1",
    "name":"name1",
    "children": [{
        "form_name": "z2",
        "name":"name2",
        "children": [{
            "form_name": "z3",
            "name":"name2"
        }]
    }]
}]

我想动态插入一个新的键值对,它应该看起来像这样:

[{
    "form_name": "z1",
    "name":"name1",
    "peopleCount": "125,678,190",
    "children": [{
        "form_name": "z2",
        "name":"name2",
        "peopleCount": "156,987",
        "children": [{
            "form_name": "z3",
            "name":"name2",
            "peopleCount": "678,098"
        }]
    }]
}]

我尝试使用如下递归,但无法达到上述结果:

function print_data($menu,  $depth) {
    foreach ($menu as $value) {
        $deepid =  '\"'.$value['form_name'].'\"';

        $api_call_var = '{{i have a apiu call proxy here}}';
        $read_data = file_get_contents($read_data);
        $read_data = json_decode($read_data, true);
        $value['peopleCount'] = $read_data['count']; // after decode read the count

        $obj_array[] = $value;
        if (is_array($value['children'])) {
            print_data ($value['children'],$depth+1);
        }
    }//end of for each

}//end of function
print_data ($json_url_data,  0);

我被困了几个小时,任何帮助将不胜感激。

【问题讨论】:

    标签: php arrays json api recursion


    【解决方案1】:

    这应该可以解决您的问题。更新同一个数组时,需要接收参数作为引用,递归更新同一个数组。

    这里是代码。我已将 123 添加为 peopleCount,但您使用 API 调用添加了价值。

    <?php
    echo "<pre>";
    $json_url_data = json_decode('[{
        "form_name": "z1",
        "name":"name1",
        "children": [{
            "form_name": "z2",
            "name":"name2",
            "children": [{
                "form_name": "z3",
                "name":"name2"
            }]
        }]
    }]', true);
    
    function print_data(&$menu) {
        foreach($menu as &$value) {
            $value['peopleCount'] = 123; // using some API call add peopleCount value here
            if(isset($value['children'])) {
                print_data($value['children']);
            }
        }
        return $menu;
    }
    
    print_r(print_data($json_url_data));
    

    这是输出。

    Array
    (
        [0] => Array
            (
                [form_name] => z1
                [name] => name1
                [children] => Array
                    (
                        [0] => Array
                            (
                                [form_name] => z2
                                [name] => name2
                                [children] => Array
                                    (
                                        [0] => Array
                                            (
                                                [form_name] => z3
                                                [name] => name2
                                                [peopleCount] => 123
                                            )
    
                                    )
    
                                [peopleCount] => 123
                            )
    
                    )
    
                [peopleCount] => 123
            )
    
    )
    

    【讨论】:

      【解决方案2】:
      $arr = json_decode('[{
          "form_name": "z1",
          "name":"name1",
          "peopleCount": "125,678,190",
          "children": [{
              "form_name": "z2",
              "name":"name2",
              "peopleCount": "156,987",
              "children": [{
                  "form_name": "z3",
                  "name":"name2",
                  "peopleCount": "678,098"
              }]
          }]
      }]', TRUE);
      function countTotal($data) {
          if(is_array($data)){
              foreach($data as $row){
                  $row['peopleCount'] = 0;///here total val
                  if(isset($row['children']) {
                      countTotal($row['children']);
                  }
              }
          }
          else {
              $data['peopleCount'] = 0;//here total val
          }
      }
      countTotal(&$arr);
      

      【讨论】:

        猜你喜欢
        • 2011-04-19
        • 2021-12-29
        • 2023-01-25
        • 2019-10-21
        • 1970-01-01
        • 1970-01-01
        • 2020-07-06
        • 2016-02-25
        • 1970-01-01
        相关资源
        最近更新 更多