繁体   English   中英

echo [标签] 所有记录 php api

[英]echo [label] of all records php api

我正在使用 API 显示 24 arrays 作为 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 个 arrays 的 [label] 数据显示为 li 项。

您可以简单地遍历所有结果,无需使用$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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM