首先,我是PHP
的新手,请原谅我。
我想采取JSON
回复并将其拆分。这将来自$_POST
变量,但我试图测试硬编码变量中的响应。问题是我甚至无法打印它以便看到我正确地开始这个。
$json =
'({
"array":
[
"Store #: 00608",
"Phone #: null",
"Address: 3014 N. SCOTTSDALE RD.",
"City: SCOTTSDALE",
"Zip: 85251",
"State: AZ",
"Height: 6`4",
"Weight: 230",
"Ethnicity: White",
"Age: 23",
"Eye Color: Blue",
"Favorite Food: Thai",
"Comments: awesome"
]
})';
$data = json_decode($json,true);
$pieces = explode(":", $data);
for ($i = 0; $i < count($data['array']); $i++) {
echo $pieces[$i];
}
当我在浏览器中启动它时,我得到一个空白屏幕,没有错误。最终目标是将这些数据存储为PHP
数组'Store #', '00608'
等等。
无论如何,我做错了什么?
答案 0 :(得分:3)
试试这个JSON。
$json =
'{
"array":
[
"Store #: 00608",
"Phone #: null",
"Address: 3014 N. SCOTTSDALE RD.",
"City: SCOTTSDALE",
"Zip: 85251",
"State: AZ",
"Height: 6`4",
"Weight: 230",
"Ethnicity: White",
"Age: 23",
"Eye Color: Blue",
"Favorite Food: Thai",
"Comments: awesome"
]
}';
请注意我拿出你的()!
答案 1 :(得分:1)
我认为这几乎就是你要找的东西。
$json =
'{
"array":
[
"Store #: 00608",
"Phone #: null",
"Address: 3014 N. SCOTTSDALE RD.",
"City: SCOTTSDALE",
"Zip: 85251",
"State: AZ",
"Height: 6`4",
"Weight: 230",
"Ethnicity: White",
"Age: 23",
"Eye Color: Blue",
"Favorite Food: Thai",
"Comments: awesome"
]
}';
$data = json_decode($json,true);
foreach($data['array'] as $piece) {
$array = explode(': ', $piece);
echo 'Key: '.$array[0].'<br />';
echo 'Value: '.$array[1].'<br />';
echo '<br />';
}
返回
Key: Store #
Value: 00608
Key: Phone #
Value: null
Key: Address
Value: 3014 N. SCOTTSDALE RD.
答案 2 :(得分:1)
您有无效的JSON,因为您在JSON字符串周围有括号。删除它们,它应该正确解析。