How to move an array in the correct way

0

Good evening guys, I'm having a lot of trouble with arrays ...

I have the following array:

array(6) {
  [0]=>
  array(1) {
    ["cep"]=>
    array(2) {
      [0]=>
      string(8) "15070650"
      [1]=>
      string(8) "03216040"
    }
  }
  [1]=>
  array(1) {
    ["endereco"]=>
    array(2) {
      [0]=>
      string(21) "Avenida Murchid Homsi"
      [1]=>
      string(17) "Rua São Raimundo"
    }
  }
  [2]=>
  array(1) {
    ["numero"]=>
    array(2) {
      [0]=>
      string(3) "275"
      [1]=>
      string(3) "275"
    }
  }
  [3]=>
  array(1) {
    ["bairro"]=>
    array(2) {
      [0]=>
      string(41) "Parque Residencial Comendador Mancor Daud"
      [1]=>
      string(16) "Vila Califórnia"
    }
  }
  [4]=>
  array(1) {
    ["cidade"]=>
    array(2) {
      [0]=>
      string(23) "São José do Rio Preto"
      [1]=>
      string(10) "São Paulo"
    }
  }
  [5]=>
  array(1) {
    ["uf"]=>
    array(2) {
      [0]=>
      string(2) "SP"
      [1]=>
      string(2) "SP"
    }
  }
}

example: suppose I need to get the zip, address, number, neighborhood, city, uf in format for example line to insert into the bank.

example:

line 1: 15070650, Avenida Murchid Homsi, 275, Parque Comendador Mancor Daud, São José do Rio Preto, SP

line 2: 03216040, Rua São Raimundo, 275, Vila California, São Paulo, SP

I tried it as follows:

for ($i = 0; $i < count($dados_endereco); $i++) {
                for ($j = 0; $j < count($dados_endereco[$i]); $j++) {
                    for ($k = 0; $k < count($dados_endereco[$i][$j]); $k++) {
                        var_dump($dados_endereco[$i][$j][$k]);  
                    }
                }
            }

but it did not return me right ...

in JSON:

[  
   {  
      "cep":[  
         "15070650",
         "03216040"
      ]
   },
   {  
      "endereco":[  
         "Avenida Murchid Homsi",
         "Rua S\u00e3o Raimundo"
      ]
   },
   {  
      "numero":[  
         "275",
         "275"
      ]
   },
   {  
      "bairro":[  
         "Parque Residencial Comendador Mancor Daud",
         "Vila Calif\u00f3rnia"
      ]
   },
   {  
      "cidade":[  
         "S\u00e3o Jos\u00e9 do Rio Preto",
         "S\u00e3o Paulo"
      ]
   },
   {  
      "uf":[  
         "SP",
         "SP"
      ]
   }
]

Thanks in advance for your help. If the question is duplicated, please put the question link in the comments.

    
asked by anonymous 07.09.2018 / 23:13

1 answer

2

Example to sort:

$json = #seu json;

$arr = json_decode($json, true);
$arr2 = array_map(function($a) {  return array_pop($a); }, $arr);
array_unshift($arr2, null);
$arr3 = call_user_func_array("array_map", $arr2);

An example of how to print:

foreach($arr3 as $k => $v) {
    echo $v[0] . ' - ' . $v[1] . ' - ' . $v[2] . ' - ' . $v[3];
    echo '<br>';
}
    
07.09.2018 / 23:49