0

首先我是一个很新的人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'等等。

无论如何,我做错了什么?

4

3 回答 3

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"
   ]
}';

注意我拿出了你的()!

于 2013-06-19T20:29:40.423 回答
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.
于 2013-06-19T20:36:54.390 回答
1

您的 JSON 无效,因为 JSON 字符串周围有括号。删除那些,它应该正确解析。

于 2013-06-19T20:37:44.450 回答