2 回答
TA贡献1780条经验 获得超5个赞
Results 重复使用索引“company”,因此如果您试图直接使用它,将会混淆整个事情。我已经测试了以下内容,并且它有效:前提是该字符串的其余部分用缺少的括号关闭。
$string = '{
"api_version":"0.4",
"results":
{
"companies":[
{
"company":
{
"name":"BILL, HILLARY \u0026 CHELSEA CLINTON FOUNDATION",
"company_number":"EXTUID_4147727",
"jurisdiction_code":"us_dc",
"incorporation_date":"1997-10-23",
"dissolution_date":"2014-11-20",
"company_type":"Non-Profit Corporation",
"registry_url":"https://corp.dcra.dc.gov/BizEntity.aspx/ViewEntityData?entityId=4147727",
"branch":"F",
"branch_status":"branch of an out-of-jurisdiction company",
"inactive":true,
"current_status":"Revoked",
"created_at":"2015-12-04T13:32:35+00:00",
"updated_at":"2018-01-16T16:14:02+00:00",
"retrieved_at":"2016-03-14T00:40:43+00:00",
"opencorporates_url":"https://opencorporates.com/companies/us_dc/EXTUID_4147727",
"previous_names":[],
"source":
{
"publisher":"District of Columbia Department of Consumer and Regulatory Affairs",
"url":"https://corp.dcra.dc.gov/BizEntity.aspx/ViewEntityData?entityId=4147727",
"retrieved_at":"2016-03-14T00:40:43+00:00"},
"registered_address":
{
"street_address":"1200 PRESIDENT CLINTON AVENUE,",
"locality":"LITTLE ROCK",
"region":"Arkansas",
"postal_code":"72201",
"country":"United States"
},
"registered_address_in_full":"1200 PRESIDENT CLINTON AVENUE,, LITTLE ROCK, Arkansas, 72201",
"industry_codes":[],
"restricted_for_marketing":null,
"native_company_number":"N00005066161"
}
},
{
"company":
{
"name":"BILL, HILLARY \u0026 CHELSEA CLINTON FOUNDATION",
"company_number":"EXTUID_2698590",
"jurisdiction_code":"us_dc",
"incorporation_date":"2016-03-03",
"dissolution_date":null,
"company_type":"Non-Profit Corporation",
"registry_url":"https://corponline.dcra.dc.gov/BizEntity.aspx/ViewEntityData?entityId=2698590",
"branch":"F",
"branch_status":"branch of an out-of-jurisdiction company",
"inactive":false,
"current_status":"Active",
"created_at":"2015-12-08T12:22:52+00:00",
"updated_at":"2020-04-19T21:38:13+00:00",
"retrieved_at":"2020-04-19T17:55:36+00:00",
"opencorporates_url":"https://opencorporates.com/companies/us_dc/EXTUID_2698590",
"previous_names":[],
"source":
{
"publisher":"District of Columbia Department of Consumer and Regulatory Affairs",
"url":"https://corponline.dcra.dc.gov/BizEntity.aspx/ViewEntityData?entityId=2698590",
"retrieved_at":"2020-04-19T17:55:36+00:00"},
"registered_address":
{
"street_address":"1200 President Clinton Avenue,",
"locality":"LITTLE ROCK",
"region":"Arkansas",
"postal_code":"72201",
"country":"United States"
},
"registered_address_in_full":"1200 President Clinton Avenue,, LITTLE ROCK, Arkansas, 72201",
"industry_codes":[],
"restricted_for_marketing":null,
"native_company_number":"292233"
}
}]}}'; //missing brackets are added here
...
$json = json_decode($string);
$companies = $json->results->companies;
$output = "
<table>
<tr>
<th>Company Name</th>
<th>Incorporation Date</th>
<th>Dissolution Date</th>
<th>Company Type</th>
<th>Registry URL</th>
<th>Current Status</th>
<th>Address</th>
</tr>
";
foreach($companies as $company){
foreach($company as $data){
$output .= "<tr>";
$output .= "<td>".$data->name."<td>";
$output .= "<td>".$data->incorporation_date."<td>";
$output .= "<td>".$data->dissolution_date."<td>";
$output .= "<td>".$data->company_type."<td>";
$output .= "<td><a href='".$data->registry_url."'></a>Registry Link<td>";
$output .= "<td>".$data->current_status."<td>";
$output .= "<td>".$data->registered_address_in_full."<td>";
}
}
$output .= "</tr></table>";
echo $output;
TA贡献1906条经验 获得超10个赞
你的第二个错误是因为你的第一个错误。您的第一个错误是因为您试图在数据中循环太深的一个级别。
尝试这个:
foreach($json['results']['companies'] as $company_values){
$company = $company_values['company'];
$output .= "<tr>";
$output .= "<td>".$company['name']."<td>";
$output .= "<td>".$company['incorporation_date']."<td>";
$output .= "<td>".$company['dissolution_date']."<td>";
$output .= "<td>".$company['company_type']."<td>";
$output .= "<td><a href='".$company['registry_url']."'></a>Registry Link<td>";
$output .= "<td>".$company['current_status']."<td>";
$output .= "<td>".$company['registered_address_in_full']."<td>";
}
基本上,您的循环需要从高 1 级开始,并且在循环内您可以从键中提取值company。
- 2 回答
- 0 关注
- 87 浏览
添加回答
举报