How to extract data in the form of indexes from this array?

Asked

Viewed 186 times

-1

I’m developing a real estate website and I will use the REST. The code below returns me one by one the properties registered in the server software as can be seen here.

$dados = array( 
    'fields'    => 
        array( 
            'Codigo', 'Cidade', 'Bairro', 'ValorVenda' 
    ), 
    'paginacao' =>  
        array( 
            'pagina'        => 1, 
            'quantidade'    => 10 
    ) 
); 

$key         =  'c9fdd79584fb8d369a6a579af1a8f681'; 
$postFields  =  json_encode( $dados ); 
$url         =  'http://sandbox-rest.vistahost.com.br/imoveis/listar?key=' . $key; 
$url        .=  '&showtotal=1&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 ); 

echo '<pre>'; 
    print_r( $result ); 
echo '<pre>';

My question is: in this case, how to extract one by one of the property arrays data in index form?

What I do know is that the $result is keeping everything inside him and putting a print_r he releases the real estate. How to dismember this information in the form of indexes, something like:

echo $result['fields']['Codigo'];

The print_r($result) print this out:

Array
(
    [62] => Array
        (
            [Codigo] => 62
            [Cidade] => Porto Alegre
            [Bairro] => Espirito Santo
            [ValorVenda] => 105000
        )

    [69] => Array
        (
            [Codigo] => 69
            [Cidade] => Porto Alegre
            [Bairro] => Central
            [ValorVenda] => 90000
        ) ...

1 answer

1


This question is very strange, since the answer is quite obvious.
Now, you will always need to specify an index for a particular record from which you want to get some information:

$registro = $results[62];
print $registro['ValorVenda'];

Even inside a loop like foreach, the index for each record is implicitly considered:

foreach ($results as $result) {
    print $result['Bairro'] . PHP_EOL;
    print $result['ValorVenda']. PHP_EOL; 
    print "---" . PHP_EOL;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.