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

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

如何在 php 中格式化數組

如何在 php 中格式化數組

PHP
哆啦的時光機 2023-05-12 10:34:24
我有這段代碼:$id = 1;$isTemplate = true;$address = "address, \n tel. 1234455";我需要以這種形式保存它:{"results": [{            "id": 1,            "isTemplate": true,            "text": "<span class=\"d-block option-head\">\n    nazwa\n</span>\n<span class=\"d-none\"> - </span>\n<span class=\"d-block option-sub\">\n        address,\n                tel. 123456\n    </span>"        ]}我怎樣才能做到?我嘗試:$resultArray = array('results' => ['id'=> 1, 'isTemplate' => true, 'text'=> '<span class="d-block option-head">\n    nazwa\n</span>\n<span class="d-none"> - </span>\n<span class="d-block option-sub">\n        address,\n                tel. 1234455\n    </span>']);但我的結果是錯誤的:Array(    [results] => Array        (            [id] => 1            [isTemplate] => 1            [text] => <span class="d-block option-head">\n    nazwa\n</span>\n<span class="d-none"> - </span>\n<span class="d-block option-sub">\n        address,\n                tel. 123455\n    </span>        ))請幫我
查看完整描述

4 回答

?
慕容森

TA貢獻1853條經驗 獲得超18個贊

$id = 1;

$isTemplate = true;

$address = "address, \n tel. 1234455";


$array = [

    'results' => [

        [

            'id' => $id,

            'isTemplate' => $isTemplate,

            'text' => $address,

        ],

    ],

];


$result = json_encode($array);


查看完整回答
反對 回復 2023-05-12
?
蝴蝶不菲

TA貢獻1810條經驗 獲得超4個贊

嘗試這個


您只需要再添加 1 個嵌套。


$resultArray = ['results' => [['id'=> 1, 'isTemplate' => true, 'text'=> '<span class="d-block option-head">\n    nazwa\n</span>\n<span class="d-none"> - </span>\n<span class="d-block option-sub">\n        address,\n                tel. 1234455\n    </span>']]];


echo '<pre>';

print_r(json_encode($resultArray));

exit;


查看完整回答
反對 回復 2023-05-12
?
慕少森

TA貢獻2019條經驗 獲得超9個贊

您的代碼缺少嵌套級別。


$resultArray = array('results' => [

    ['id' => 1, 'isTemplate' => true, 'text' => '<span class="d-block option-head">\n    nazwa\n</span>\n<span class="d-none"> - </span>\n<span class="d-block option-sub">\n        address,\n                tel. 1234455\n    </span>']

]);

var_dump($resultArray, json_encode($resultArray));


查看完整回答
反對 回復 2023-05-12
?
慕桂英4014372

TA貢獻1871條經驗 獲得超13個贊

 $id = 1;

        $isTemplate = true;

        $address = "address, \n tel. 1234455";

        $text= '<span class="d-block option-head">\n    nazwa\n</span>\n<span class="d-none"> - </span>\n<span class="d-block option-sub">\n        address,\n';

        $array[] = ['id'=>$id ,'isTemplate'=>$isTemplate, 'address' => $address,'text'=>$text];


        $value = json_encode($array);

$value 將保存所需的結果......


查看完整回答
反對 回復 2023-05-12
  • 4 回答
  • 0 關注
  • 232 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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