我有一個多維數組,我試圖解析通過,它拋出奇怪的結果(或者我只是忽略了一些簡單的東西)。這是從一開始就json_decode樣本的print_r:解析通過PHP多維數組
Array
(
[DateGenerated] => 2014-01-12T19:30:21.897
[Corporation] => Array
(
[CorporateBuilderNumber] => MHI
[CorporateState] => TX
[CorporateName] => McGuyer Homebuilders, Inc.
[Builder] => Array
(
[0] => Array
(
[BuilderNumber] => COV
[BrandName] => Coventry Homes
[ReportingName] => Coventry Homes
[DefaultLeadsEmail] => [email protected]
[BuilderWebsite] => http://www.coventryhomes.com
[Subdivision] => Array
當使用此代碼:
<?php
mysql_connect("localhost", "", "");
mysql_select_db("");
$json_data = file_get_contents('mhi.json');
$json = json_decode($json_data, true);
foreach ($json as $value) {
echo $value['CorporateBuilderNumber'] . "<br />";
echo $value['CorporateState'] . "<br />";
echo $value['CorporateName'] . "<br />";
}
?>
我得到這個作爲輸出:
2
2
2
MHI
TX
McGuyer Homebuilders, Inc.
如果是,2來從?
'的foreach($ JSON作爲$鍵=> $值)'? – elclanrs
在foreach語句中省略'$ key'沒有問題。我覺得2可能來自一些較早的代碼。您的foreach聲明之前您有什麼? – Scopey
我意識到發生了什麼,並在下面提交了答案。 – Scopey