为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
小唯快跑啊 2023-07-15 15:42:55
我通过 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
查看完整描述

2 回答

?
慕盖茨4494581

TA贡献1850条经验 获得超11个赞

只需在 URL 中使用多维数组,例如:


test.php?key1=1&key2=2&myserver[state]=1&myserver[running]=2&myserver[mem]=3

如此简单的脚本


<?php

echo '<pre>';

echo json_encode($_GET, JSON_PRETTY_PRINT);

会给你


{

    "key1": "1",

    "key2": "2",

    "myserver": {

        "state": "1",

        "running": "2",

        "mem": "3"

    }

}

当然,如果需要,您也可以使用具有相同命名规则的 POST 请求。


查看完整回答
反对 回复 2023-07-15
?
湖上湖

TA贡献2003条经验 获得超2个赞

为了创建嵌套的 JSON 对象,您需要在数组中创建数组。


例如


$example = [

    'key1' => 'foo',

    'key2' => 'bar',

    'key3' => [

        'subkey1' => 'foo',

        'subkey2' => 'bar',

    ],

];

当运行它时json_encode(),它会导致


{

  "key1": "foo",

  "key2": "bar",

  "key3": {

    "subkey1": "foo",

    "subkey2": "bar"

  }

}

也没有必要像这样加载表单数据 –


$formdata = [

    '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'],

];

因为$_POST已经包含您正在尝试重新创建的结构。您只需将发布数据分配给新变量即可。


另一方面,我强烈建议您查看 PSR PHP 标准,它们将极大地帮助提高代码可读性和代码结构:) https://www.php-fig.org/psr/


查看完整回答
反对 回复 2023-07-15
  • 2 回答
  • 0 关注
  • 95 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信