How to loop to load Json result

Asked

Viewed 63 times

-1

People are in need of help, I am carrying some information from Json, as follows::

<?php

$json_data = json_decode(file_get_contents('arquivo3.json'), true);

foreach ($json_data["Result"] as $data) {

	
	echo '<br>Endereço: ' .$data["CreditData"][0]["Addresses"][0]["AddressMain"];
	echo '<br>Número: ' .$data["CreditData"][0]["Addresses"][0]["Number"];
	echo '<br>Complemento: ' .$data["CreditData"][0]["Addresses"][0]["Complement"];
	echo '<br>Cidade: ' .$data["CreditData"][0]["Addresses"][0]["City"];
	echo '<br>CEP: ' .$data["CreditData"][0]["Addresses"][0]["ZipCode"];
	echo '<br>Familiar: ' .$data["CreditData"][0]["Addresses"][0]["Neighborhood"];
	echo '<br>Estado: ' .$data["CreditData"][0]["Addresses"][0]["State"];
	echo '<br>País: ' .$data["CreditData"][0]["Addresses"][0]["Country"];
	
    echo '<br>';
	
// NÃO QUERIA REPETIR A LINHA ABAIXO E COLOCAR AQUI UM LOOP PARA AUTOMATICAMENTE CARREGAR QUANTO FOR NECESSÁRIOS DE ENDEREÇOS

	echo '<br>Endereço: ' .$data["CreditData"][0]["Addresses"][1]["AddressMain"];
	echo '<br>Número: ' .$data["CreditData"][0]["Addresses"][1]["Number"];
	echo '<br>Complemento: ' .$data["CreditData"][0]["Addresses"][1]["Complement"];
	echo '<br>Cidade: ' .$data["CreditData"][0]["Addresses"][1]["City"];
	echo '<br>CEP: ' .$data["CreditData"][0]["Addresses"][1]["ZipCode"];
	echo '<br>Familiar: ' .$data["CreditData"][0]["Addresses"][1]["Neighborhood"];
	echo '<br>Estado: ' .$data["CreditData"][0]["Addresses"][1]["State"];
	echo '<br>País: ' .$data["CreditData"][0]["Addresses"][1]["Country"];
	
    echo '<br>';

}
?>

QUESTION:

As you can see, I need him to bring the address of a certain person, but some people have more than 1 registered address, so he can bring 1 or 2 or even more than 5 different addresses. How do I create a loop so I don’t have to keep repeating lines?

  • Dejavu -> https://answall.com/questions/371710/como-ler-esse-exemplo-de-json-correctment/371711

1 answer

2


You’re already using a loop, aren’t you? Don’t just nest another loop inside it?

foreach ($json_data["Result"] as $data) {
    foreach ($data["CreditData"][0]["Addresses"] as $address) {

        echo '<br>Endereço: ' .$address["AddressMain"];
        echo '<br>Número: ' .$address["Number"];
        echo '<br>Complemento: ' .$address["Complement"];
        echo '<br>Cidade: ' .$address["City"];
        echo '<br>CEP: ' .$address["ZipCode"];
        echo '<br>Familiar: ' .$address["Neighborhood"];
        echo '<br>Estado: ' .$address["State"];
        echo '<br>País: ' .$address["Country"];

        echo '<br>';
    }
}

Browser other questions tagged

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