对于此请求结果,还是对于Foreach?

时间:2019-07-15 14:55:10

标签: laravel for-loop foreach

返回mi Laravel请求我得到他的送葬服务:

{
"_method": "POST",
"_token": null,
"nome": "AUTO ESCOLA GUILHERMITTI & LOBANCO LTDA",
"cnpj": "00635344000177",
"novoSegmento": null,
"natureza": "206-2",
"total": "49993",
"socio-0": "ELIANA CRISTINA GUILHERMITTI RODRIGUES",
"socio-1": "SUZANA MARQUES LOBANCO",
"socio-2": "AMARILDO APARECIDO RODRIGUES",
"socio-3": "VALDEMIR FRANCISCO DA COSTA",
"socio-4": "ANDRE LUIS LOBANCO",
"nomeFantasia": null,
"IE": null,
"logradouro": null,
"numero": "1408",
"complemento": null,
"bairro": null,
"uf": "SP",
"cidade": "Bady Bassitt",
"ddd": null,
"telefone": null,
"ddd1": null,
"celular": null,
"receita1": "0.00",
"referenciaClientes": [
{
"nome": null
},
{
"ddd": null
},
{
"telefone": null
},
{
"ramal": null
},
{
"endereco": null
},
{
"bairro": null
},
{
"uf": null
},
{
"cidade": null
},
{
"cep": null
},
{
"email": null
}
],
"BancoreferenciaClientes": [
{
"contato": null
}
],
"referenciaCLientes": [
{
"complento": null
}
],
"banco": null,
"agencia": null,
"contaCorrente": null,
"bancoContato": null,
"telefoneBanco": null,
"cidadeBanco": null,
"imoveis": [
{
"enderecoBem": null
},
{
"areaBem": null
},
{
"valorBem": "0.00"
},
{
"cidadeBem": null
}
],
"aceite": "1"
}

我需要保存所有项目,但是不知道如何正确处理socio-*。它是可变的,我永远也不会知道它会发生多少次。

我对如何处理这个问题有点迷茫... 我以为我应该使用以下次数来获得“社交”发生次数:

$s = substr_count($request, 'socio');

现在我不知道我是否使用For或Foreach以及如何使用它。 有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

我希望您的数据格式为 Json ,所以首先将其更改为 array ,然后使用 foreach

$array_data = json_decode($json,true);

$data = [];
foreach($array_data as $key => $value)
{
   // first we are checking key should be not array and then socio- is exists
   if(!is_array($key) && strpos($key, 'socio-') !== false)
   {
      $number = substr($value, strlen('socio-'));
      $data[$number] = $value;
   }
}
dd($data);

这样您将得到这样的响应

[
 0 =>'ELIANA CRISTINA GUILHERMITTI RODRIGUES',
 1 =>'SUZANA MARQUES LOBANCO',
 2 =>'AMARILDO APARECIDO RODRIGUES',
 3 =>'VALDEMIR FRANCISCO DA COSTA',
 4 =>'ANDRE LUIS LOBANCO',
];