亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 161 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號