2
The code below returns real estate code results from a Rest API, and it comes with array within array, expected to treat the list to get only a Code array with its value, e.g.:
<?php
$dados = 'Codigo';
$key = 'c9fdd79584fb8d369a6a579af1a8f681'; //Informe sua chave aqui
$postFields = json_encode( $dados );
$url = 'http://sandbox-rest.vistahost.com.br/imoveis/listar?key=' . $key;
$url .= '&pesquisa=' . $postFields;
$ch = curl_init($url);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER , array( 'Accept: application/json' ) );
$result = curl_exec( $ch );
$result = json_decode( $result, true );
$totalImovel = count($result);
echo '<pre>';
print_r( $result );
echo '</pre>';
Upshot:
Array
(
[3152] => Array
(
[Codigo] => 3152
)
[3153] => Array
(
[Codigo] => 3153
)
[2448] => Array
(
[Codigo] => 2448
)
[3141] => Array
(
[Codigo] => 3141
)
[3151] => Array
(
[Codigo] => 3151
)
[25] => Array
(
[Codigo] => 25
)
[38] => Array
(
[Codigo] => 38
)
[3378] => Array
(
[Codigo] => 3378
)
[42] => Array
(
[Codigo] => 42
)
[54] => Array
(
[Codigo] => 54
)
)
How to make this result look like the example below, this could not do:
{
'Codigo': 3152,
........
}
And I tried to print the results in a for:
$totalImovel = count($result);
for($i=0;$i<$totalImovel;$i++) {
echo $i."<br>";
echo $result[$i]['Codigo']."<br>";
}
But I don’t get any results.
Actually, it’s called an associative array (something like that), where you don’t use the index (starting with 0) but a
key
(in your case the code) that is linked to the value.– Bruno Romualdo