所有记录的回显[标签] php api
Posted
技术标签:
【中文标题】所有记录的回显[标签] php api【英文标题】:echo [label] of all records php api 【发布时间】:2022-01-24 05:06:29 【问题描述】:我正在使用一个 API,它将 24 个数组显示为 json 解码数据,但我正在显示数据,只有第二条和第三条记录, 有人可以帮助显示所有 24 个数组 [标签] 数据。
原始数据:
stdClass Object
(
[success] => 1
[result] => stdClass Object
(
[matches] => Array
(
[0] => stdClass Object
(
[label] => E 0th St, Tennessee, IL
[components] => stdClass Object
(
[streetName] => 0th
[predirectional] => E
[suffix] => St
)
[city] => Tennessee
[deliveryLine] => E 0th St
[state] => IL
)
[1] => stdClass Object
(
[label] => E 1 Rd, Tecumseh, KS
[components] => stdClass Object
(
[streetName] => 1
[predirectional] => E
[suffix] => Rd
)
[city] => Tecumseh
[deliveryLine] => E 1 Rd
[state] => KS
)
[2] => stdClass Object
(
[label] => E 100 N, Teton, ID
[components] => stdClass Object
(
[streetName] => 100
[predirectional] => E
[postdirectional] => N
)
[city] => Teton
[deliveryLine] => E 100 N
[state] => ID
)
[3] => stdClass Object
(
[label] => E 100 N, Tipton, IN
[components] => stdClass Object
(
[streetName] => 100
[predirectional] => E
[postdirectional] => N
)
[city] => Tipton
[deliveryLine] => E 100 N
[state] => IN
)
[4] => stdClass Object
(
[label] => E 100 N, Torrey, UT
[components] => stdClass Object
(
[streetName] => 100
[predirectional] => E
[postdirectional] => N
)
[city] => Torrey
[deliveryLine] => E 100 N
[state] => UT
)
[5] => stdClass Object
(
[label] => E 100 N, Tremonton, UT
[components] => stdClass Object
(
[streetName] => 100
[predirectional] => E
[postdirectional] => N
)
[city] => Tremonton
[deliveryLine] => E 100 N
[state] => UT
)
[6] => stdClass Object
(
[label] => E 100 S, Tipton, IN
[components] => stdClass Object
(
[streetName] => 100
[predirectional] => E
[postdirectional] => S
)
[city] => Tipton
[deliveryLine] => E 100 S
[state] => IN
)
[7] => stdClass Object
(
[label] => E 100 S, Tooele, UT
[components] => stdClass Object
(
[streetName] => 100
[predirectional] => E
[postdirectional] => S
)
[city] => Tooele
[deliveryLine] => E 100 S
[state] => UT
)
[8] => stdClass Object
(
[label] => E 100 S, Torrey, UT
[components] => stdClass Object
(
[streetName] => 100
[predirectional] => E
[postdirectional] => S
)
[city] => Torrey
[deliveryLine] => E 100 S
[state] => UT
)
[9] => stdClass Object
(
[label] => E 100 S, Tremonton, UT
[components] => stdClass Object
(
[streetName] => 100
[predirectional] => E
[postdirectional] => S
)
[city] => Tremonton
[deliveryLine] => E 100 S
[state] => UT
)
[10] => stdClass Object
(
[label] => E 1000 N, Tooele, UT
[components] => stdClass Object
(
[streetName] => 1000
[predirectional] => E
[postdirectional] => N
)
[city] => Tooele
[deliveryLine] => E 1000 N
[state] => UT
)
[11] => stdClass Object
(
[label] => E 10000 N, Tetonia, ID
[components] => stdClass Object
(
[streetName] => 10000
[predirectional] => E
[postdirectional] => N
)
[city] => Tetonia
[deliveryLine] => E 10000 N
[state] => ID
)
[12] => stdClass Object
(
[label] => E 10000 N, Tridell, UT
[components] => stdClass Object
(
[streetName] => 10000
[predirectional] => E
[postdirectional] => N
)
[city] => Tridell
[deliveryLine] => E 10000 N
[state] => UT
)
[13] => stdClass Object
(
[label] => E 10000th N, Tridell, UT
[components] => stdClass Object
(
[streetName] => 10000th
[predirectional] => E
[postdirectional] => N
)
[city] => Tridell
[deliveryLine] => E 10000th N
[state] => UT
)
[14] => stdClass Object
(
[label] => E 1000 North Rd, Taylorville, IL
[components] => stdClass Object
(
[streetName] => 1000 North
[predirectional] => E
[suffix] => Rd
)
[city] => Taylorville
[deliveryLine] => E 1000 North Rd
[state] => IL
)
[15] => stdClass Object
(
[label] => E 1000 North Rd, Thawville, IL
[components] => stdClass Object
(
[streetName] => 1000 North
[predirectional] => E
[suffix] => Rd
)
[city] => Thawville
[deliveryLine] => E 1000 North Rd
[state] => IL
)
[16] => stdClass Object
(
[label] => E 1000 North Rd, Tower Hill, IL
[components] => stdClass Object
(
[streetName] => 1000 North
[predirectional] => E
[suffix] => Rd
)
[city] => Tower Hill
[deliveryLine] => E 1000 North Rd
[state] => IL
)
[17] => stdClass Object
(
[label] => E 1000S Rd, Pembroke Township, IL
[components] => stdClass Object
(
[streetName] => 1000S
[predirectional] => E
[suffix] => Rd
)
[city] => Pembroke Township
[deliveryLine] => E 1000S Rd
[state] => IL
)
[18] => stdClass Object
(
[label] => E 1000th S, Twelve Mile, IN
[components] => stdClass Object
(
[streetName] => 1000th
[predirectional] => E
[postdirectional] => S
)
[city] => Twelve Mile
[deliveryLine] => E 1000th S
[state] => IN
)
[19] => stdClass Object
(
[label] => E 100th Ave, Tempe, AZ
[components] => stdClass Object
(
[streetName] => 100th
[predirectional] => E
[suffix] => Ave
)
[city] => Tempe
[deliveryLine] => E 100th Ave
[state] => AZ
)
[20] => stdClass Object
(
[label] => E 100th Ave, Thornton, CO
[components] => stdClass Object
(
[streetName] => 100th
[predirectional] => E
[suffix] => Ave
)
[city] => Thornton
[deliveryLine] => E 100th Ave
[state] => CO
)
[21] => stdClass Object
(
[label] => E 100th Ct, Thornton, CO
[components] => stdClass Object
(
[streetName] => 100th
[predirectional] => E
[suffix] => Ct
)
[city] => Thornton
[deliveryLine] => E 100th Ct
[state] => CO
)
[22] => stdClass Object
(
[label] => E 100th Ct, Tulsa, OK
[components] => stdClass Object
(
[streetName] => 100th
[predirectional] => E
[suffix] => Ct
)
[city] => Tulsa
[deliveryLine] => E 100th Ct
[state] => OK
)
[23] => stdClass Object
(
[label] => E 100th Dr, Thornton, CO
[components] => stdClass Object
(
[streetName] => 100th
[predirectional] => E
[suffix] => Dr
)
[city] => Thornton
[deliveryLine] => E 100th Dr
[state] => CO
)
[24] => stdClass Object
(
[label] => E 100th Dr, Tulsa, OK
[components] => stdClass Object
(
[streetName] => 100th
[predirectional] => E
[suffix] => Dr
)
[city] => Tulsa
[deliveryLine] => E 100th Dr
[state] => OK
)
)
)
)
只显示这两条记录:
E 1 Rd, Tecumseh, KS
E 100 N, Tipton, IN
用于显示数据的php代码:
<?php
$term = $_POST["keyword"];
// Initialize a CURL session.
$url = 'https://example.com/ws/addresses/match?term='.$term;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: s9-2659fbc5-036c-47-testtt-e2d']);
// Return Page contents.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
echo "<pre>";
$result = curl_exec($ch);
$data = json_decode($result);
?>
<ul id="title">
<?php
$i = 0;
$j = 0;
foreach($data as $street)
?>
<li onClick="selectCountry('<?php echo $data->result->matches[$i++]->label; ?>');"><?php echo $data->result->matches[$i++]->label; ?></li>
<?php ?>
</ul>
我只想将所有 24 个数组的 [label] 数据显示为 li 项。
【问题讨论】:
【参考方案1】:您可以简单地遍历所有结果,无需使用$i
通过索引来引用它们:
// ... your curl code ...
$data = json_decode($result); ?>
<ul id="title">
<?php
foreach ($data->result->matches as $street) ?>
<li onClick="selectCountry('<?php echo $street->label; ?>');><?php echo $street->label; ?></li>
<?php ?>
</ul>
【讨论】:
非常感谢,我也在找。以上是关于所有记录的回显[标签] php api的主要内容,如果未能解决你的问题,请参考以下文章