Treat Json return array with CURL

Asked

Viewed 651 times

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.

3 answers

3

Solution:

echo '<pre>';
//print_r( $result );
foreach ($result as $item) {
    echo $item['Codigo'];
    echo '<br>';
}
echo '</pre>';

What it takes is that when you call "for" with index 0 the object is not found, because the index is already coming as "3152", etc. Also you can take the direct code in the index this way:

echo '<pre>';
foreach ($result as $key => $item) {
    echo 'Código: '. $key;
    echo '<br>';
}
echo '</pre>';
  • 1

    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.

3


Another option to create an array from another array using a key like index would be to use the function array_column of PHP.

$lista = array(
    3152=>array('codigo'=>3152),
    3153=>array('codigo'=>3153),
    2448=>array('codigo'=>2448),
    3141=>array('codigo'=>3141),
    3151=>array('codigo'=>3151),
);

$arr2= array_column($lista, 'codigo');
  • Made surgeon, clean and precise, thanks for your help.

2

You can do it this way:

<?php
    $lista = array(
        3152=>array('codigo'=>3152),
        3153=>array('codigo'=>3153),
        2448=>array('codigo'=>2448),
        3141=>array('codigo'=>3141),
        3151=>array('codigo'=>3151),
    );
    $lista2 = array();
    foreach($lista as $chave => $valor){
        array_push($lista2,$valor);
    }

    $jsonObj = json_encode($lista2);
    print_r($jsonObj);
?>

If you did not want to work with JSON you can use the contents of the array $lista2

  • It is returning Json from an @Murilo API, and is transforming json into an array (json_decode($result, true) -> the true parameter transforms json into array).

  • @Brunoromualdo exactly, he gets a JSON, and turns it into an array to try to treat like he said in the question, in my answer, in addition to doing exactly what he asked in the question, I only included his option to go back to JSON, what the problem is?

  • It’s more a matter of turning into json again, I don’t think he wants to bring a json turn into array and go back to json again.

  • @Brunoromualdo why in my answer I put at the end that if he does not want to use Jason to use the $Lista2 data

  • It comes from Json to Array, it makes no sense to repeat the process, but thank you for your contribution.

Browser other questions tagged

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