将JSON格式化为数组$ variable

时间:2019-04-28 23:48:47

标签: php arrays json

我有一个具有以下内容的大型JSON:

  {"createTx": { 
    "inputs": [{
    "address": "mtFYoSowT3i649wnBDYjCjewenh8AuofQb",
    "value": 0.00009
}],
"outputs": [{
    "address": "mmskWH7hG9CJNzb16JaVFJyWdgAwcVEAkz",
    "value": 0.00018
}],
"fee":  {
    "address": "mmskWH7hG9CJNzb16JaVFJyWdgAwcVEAkz",
    "value": 0.00023141
}},  "wifs" : [
    "cUGddnJmuzfzpWXNwt1SRnQ8GMqZdQ1vg8BtwjG8f275pvExPzaX", "cSEjySAREyai8eQhgoqixzmxCeSP8QtbwHxptL8ijofg68ZMjoud", "cV2u6dqfiQthWfPixJ7ucFW5Tza1ubLr6ipM35vSTy9xGEKbCbaJ"]}

为了在CURL POST中运行它,我需要将其放入$ variable中,并采用以下格式:

  $data = array("createTX" => "","inputs" => array ("1MfyBywPTSj9aAPr8cccCTcch71fd4vkDA" ) );

我需要在$ data变量中包含整个JSON。

1 个答案:

答案 0 :(得分:0)

您正在寻找json_decode()

为了直接在PHP中使用此JSON字符串,请使用PHP的NOWDOCHEREDOC语法,如下所示:

$json = <<< JSON
{"createTx": {
    "inputs": [{
    "address": "mtFYoSowT3i649wnBDYjCjewenh8AuofQb",
    "value": 0.00009
}],
"outputs": [{
    "address": "mmskWH7hG9CJNzb16JaVFJyWdgAwcVEAkz",
    "value": 0.00018
}],
"fee":  {
    "address": "mmskWH7hG9CJNzb16JaVFJyWdgAwcVEAkz",
    "value": 0.00023141
}},  "wifs" : [
    "cUGddnJmuzfzpWXNwt1SRnQ8GMqZdQ1vg8BtwjG8f275pvExPzaX", "cSEjySAREyai8eQhgoqixzmxCeSP8QtbwHxptL8ijofg68ZMjoud", "cV2u6dqfiQthWfPixJ7ucFW5Tza1ubLr6ipM35vSTy9xGEKbCbaJ"]}
JSON;

$data = json_decode($json, true);