Array within Array

Asked

Viewed 161 times

1

Good night,

I am consuming a webservice and get the data in json with Curl. I have the data stored in the variable $data. What I need is this:

Let’s assume that the variable $data has the following data:

$data = [ {"nome":"Edileuza","CPF":"009876543-00"}{"nome":"Cleuza","CPF":"123456789-00"} ]

So as you can see I have the CPF inside that array. I need to consume another webservice that takes the number and brings other information. And with this information I will have another array that I want to put inside the first array.

I’m not able to think of a way to do this but I think I should foreach the $date variable. Below I will show what I think would be right and if anyone can help me I will be grateful.

$ch = curl_init($link);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

$data = curl_exec($ch);

curl_close($ch);


$data = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($data));

$data = json_decode($data, true);

This is where I don’t know what to do


foreach ($data as $key => $value) {

$cpf = $value["CPF"]

$link2 = "https://www.siteexemplo.com.br/ws/$cpf"

$ch2 = curl_init($link2);

curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);

$data2 = curl_exec($ch2);

curl_close($ch2);


$data2 = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($data2));

$data2 = json_decode($data2, true);

}

the result would be

$data2 = [ {"Endereco":"Rua 1"}]

Dai would like the Address to go to the $data array to look like this:

$data = [ {"nome":"Edileuza","CPF":"009876543-00", "Endereco":"Rua 1"}{"nome":"Cleuza","CPF":"123456789-00", "Endereco":"Rua 2"}

Thanks for your help.

  • Where does "Street 2" come from? From what you’ve shown, the $data2 only returns "Street 1".

  • Hello sam, the result Street 1 comes from the consultation, the second webservice makes the query 1 by one. Then at the end I passed the result of the foreach loop that would appear Street 1 and Street 2

  • In this case it would be something like this: array_push($data, result of the $data2 foreach) and continue the Loop until you go through the whole array

  • Use as follows: https://pastebin.com/dK8TVpFa

  • Valdeir, thank you for your help. Your code seems to be the solution but there is no foreach in it. Why do I have to go through the $data array by picking up the index numbers, it’s over 300.

  • I’ll try something here and put it on after

Show 1 more comment

1 answer

0

Try to do it that way:

$data = '[{"nome": "Edileuza", "CPF": "009876543-00"},{"nome": "Cleuza","CPF": "123456789-00"}]';
$data = json_decode($data, true);

$novos_dados = []; //ou array(); dependendo da versao do PHP
foreach ($data as $key => $value){

    //$data2 = '[{"Endereco":"Rua 1", "Cidade" : "Minha Cidade"}]'; //buscar data2 do webservice
    $data2 = '[{"Endereco":"Rua 1"}]'; //buscar data2 do webservice

    $data2 = json_decode($data2, true);

    if($data2) {
        foreach ($data2 as $key2 => $val2) { //adiciona todos os valores de data2 caso este tenha mais de uma informação. Ex: [{"Endereco": "Rua 1", "Cidade" : "Minha Cidade"}]
            $value += $val2; //concatena os arrays - o mesmo que $value = $value + $val2;
        }
    }
    $novos_dados[] = $value;
}

echo json_encode($novos_dados);

Browser other questions tagged

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