How to scroll through an array the right way

Asked

Viewed 95 times

0

Good evening guys, I’m having an immense difficulty regarding 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 pick up the zip code, address, number, neighborhood, city, Uf line for example to insert into the bank.

example:

line 1: 15070650, Avenida Murchid Homsi, 275, Parque Residencial 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 this way:

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 you didn’t call me right back...

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 for your help. If the question is duplicated, please put the question link in the comments.

  • To work with Arrays, give preference to foreach, as it facilitates and can manipulate both the key and the value.

  • Rbz first thanks for the tip. With foreach how do I achieve these values? I prefer with for because I couldn’t get by foreach.

  • Can you put the array in string format? The way it is I can’t test.

  • Rbz, no, this data comes from a front input array.. = /, I’ll post it as json for you

  • RBZ, posted in json

  • worked out!!! thank you very much! I will understand this code now. Thanks very much!

  • 1

    Time you take a break, I’ll edit and explain every step. ;)

Show 2 more comments

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>';
}

Browser other questions tagged

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