Fill a Loop Multidimensional Array from 3 separate PHP arrays

Asked

Viewed 343 times

-1

Someone knows a way to fill a Multidimensional Array with a Loop structure from 3 separate PHP arrays?

Separate arrays are in the following structure:

inserir a descrição da imagem aqui

After filling with some loop structure a single array must have the following structure below:

inserir a descrição da imagem aqui

I tried with 2 foreach and 2 for but none worked.

I stand by.

  • Opa Anderson Carlos Woss, this solution there served, thanks for the tip!

1 answer

0

Hello, I managed to solve using two foreach, see if it helps you:

$dados = [

  'id' => [1,2,3,4],
  'ocorrencia' => [10,13,17,18],
  'justificativa' => ['1232132131','232323232','435454354','565656565']
];


foreach($dados as $chave => $d ){

  //$chave = id, ocorrencia, justificativa

  foreach($d as $indice => $valor){

    //$indice = 0,1,2,3
    //$valor = 10,13,17,18

    /* $novoArray[0][id] = 1;
       $novoArray[1][id] = 2;
       $novoArray[2][id] = 3;
       $novoArray[3][id] = 4;
       $novoArray[0][ocorrencia] = 10;
       $novoArray[1][ocorrencia] = 13;
       $novoArray[2][ocorrencia] = 17; ... E por ai vai */

    $novoArray[$indice][$chave] = $valor; 

   }
}

If you want to test, just copy and paste this code and play here on http://phptester.net/

Browser other questions tagged

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