PHP 将类似名称的变量转换为 json

我通过 url 查询字符串变量获取,例如:


myserver_state=1&myserver_running=2&myserver_mem=3


目前我正在添加到现有的 json,例如:


{

   "key1": "1",

   "key2": "2",

   "key3": "3",

   "myserver_state": "1",

   "myserver_running": "2",

   "myserver_mem": "3"

}

我真的想要这样:


{

   "key1": "1",

   "key2": "2",

   "key3": "3",

   "myserver": {

      "state": "1",

      "running": "2",

      "mem": "3"

   }

}

我用它来加载它们:


        $formdata = array(

          'state'=> $_POST['state'],

          'uassip'=> $_POST['uassip'],

          'uassipport'=> $_POST['uassipport'],

          'c_uacminrtpport'=> $_POST['c_uacminrtpport'],

          'c_uacmaxrtpport'=> $_POST['c_uacmaxrtpport'],

          'c_cps'=> $_POST['c_cps'],

          'c_totalcalls'=> $_POST['c_totalcalls'],

          'c_maxchannels'=> $_POST['c_maxchannels'],

          'c_duration'=> $_POST['c_duration'],

          'c_to'=> $_POST['c_to'],

          'c_uacxml'=> $_POST['c_uacxml']

        );


        echo "fromdata: <br>"; echo var_dump($formdata) .  "<br><hr>";

        if(file_put_contents('testconfig.json', json_encode($formdata) )) echo 'OK';

        else echo 'Unable to save data in "testconfig.json"';

非常感谢!


编辑:


我尝试过以下评论:


status.php?server1[当前状态]=10


这实际上可以:


    "c_uacxml": "telnyx-uac-invite-ok.xml",

    "server1": {

        "current_state": "10"

    }

}

这很棒,但是,如果我想添加这样的元素:status.php?server1[current_mem]=1


这实际上取代了整个server1


    "c_uacxml": "telnyx-uac-invite-ok.xml",

    "server1": {

        "current_mem": "10"

    }

}

我失去了已经存在的 current_state


小唯快跑啊
浏览 101回答 2
2回答

慕盖茨4494581

只需在 URL 中使用多维数组,例如:test.php?key1=1&key2=2&myserver[state]=1&myserver[running]=2&myserver[mem]=3如此简单的脚本<?phpecho '<pre>';echo json_encode($_GET, JSON_PRETTY_PRINT);会给你{&nbsp; &nbsp; "key1": "1",&nbsp; &nbsp; "key2": "2",&nbsp; &nbsp; "myserver": {&nbsp; &nbsp; &nbsp; &nbsp; "state": "1",&nbsp; &nbsp; &nbsp; &nbsp; "running": "2",&nbsp; &nbsp; &nbsp; &nbsp; "mem": "3"&nbsp; &nbsp; }}当然,如果需要,您也可以使用具有相同命名规则的 POST 请求。

湖上湖

为了创建嵌套的 JSON 对象,您需要在数组中创建数组。例如$example = [&nbsp; &nbsp; 'key1' => 'foo',&nbsp; &nbsp; 'key2' => 'bar',&nbsp; &nbsp; 'key3' => [&nbsp; &nbsp; &nbsp; &nbsp; 'subkey1' => 'foo',&nbsp; &nbsp; &nbsp; &nbsp; 'subkey2' => 'bar',&nbsp; &nbsp; ],];当运行它时json_encode(),它会导致{&nbsp; "key1": "foo",&nbsp; "key2": "bar",&nbsp; "key3": {&nbsp; &nbsp; "subkey1": "foo",&nbsp; &nbsp; "subkey2": "bar"&nbsp; }}也没有必要像这样加载表单数据 –$formdata = [&nbsp; &nbsp; 'state' => $_POST['state'],&nbsp; &nbsp; 'uassip' => $_POST['uassip'],&nbsp; &nbsp; 'uassipport' => $_POST['uassipport'],&nbsp; &nbsp; 'c_uacminrtpport' => $_POST['c_uacminrtpport'],&nbsp; &nbsp; 'c_uacmaxrtpport' => $_POST['c_uacmaxrtpport'],&nbsp; &nbsp; 'c_cps' => $_POST['c_cps'],&nbsp; &nbsp; 'c_totalcalls' => $_POST['c_totalcalls'],&nbsp; &nbsp; 'c_maxchannels' => $_POST['c_maxchannels'],&nbsp; &nbsp; 'c_duration' => $_POST['c_duration'],&nbsp; &nbsp; 'c_to' => $_POST['c_to'],&nbsp; &nbsp; 'c_uacxml' => $_POST['c_uacxml'],];因为$_POST已经包含您正在尝试重新创建的结构。您只需将发布数据分配给新变量即可。另一方面,我强烈建议您查看 PSR PHP 标准,它们将极大地帮助提高代码可读性和代码结构:) https://www.php-fig.org/psr/
打开App,查看更多内容
随时随地看视频慕课网APP