I am using an API which is showing 24 arrays as json decode data, but I am displaying data, only second and third record, can someone help to display all 24 arrays[label] data.
Raw Data:
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
)
)
)
)
Only these two records are displaying:
E 1 Rd, Tecumseh, KS
E 100 N, Tipton, IN
PHP Code used to display data:
<?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>
I only wanted to display [label] data of all the 24 arrays as li item.
CodePudding user response:
You can simply iterate over all results, no need to reference them by index with $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>