Add matching information that is in different arrays in the same array

4
When the user has a submit in the form, I get the following arrays (In case I had 3 products in form, if I only have 1, in the 4 arrays I would only have index 0 if I had 5 products in the 4 arrays would have 0, 1, 2, 3 and 4 indexes)

'tempero' => 
    array (size=3)
      0 => string 'Ketchup' (length=7)
      1 => string 'Mustard' (length=7)
      2 => string 'Barbecue' (length=8)
  'quantidade' => 
    array (size=3)
      0 => string '1' (length=1)
      1 => string '2' (length=1)
      2 => string '3' (length=1)
  'frequencia' => 
    array (size=3)
      0 => string 'FKetchup' (length=2)
      1 => string 'FMustard' (length=2)
      2 => string 'FBarbecue' (length=2)
  'combo' => 
    array (size=3)
      0 => string 'CKetchup' (length=2)
      1 => string 'CMustard' (length=2)
      2 => string 'CBarbecue' (length=2)

My problem is next, I would need to put the corresponding items in arrays, in case I would need something like this:

 'tempero1' => 
    array (size=3)
      0 => string 'Ketchup' (length=7)
      1 => string '1' (length=7)
      2 => string 'FKetchup' (length=8)
      3 => string 'CKetchup' (length=8)
  'tempero2' => 
    array (size=3)
      0 => string 'Mustard' (length=7)
      1 => string '2' (length=7)
      2 => string 'FMustard' (length=8)
      3 => string 'CMustard' (length=8)
  'tempero3' => 
    array (size=3)
      0 => string 'Barbecue' (length=7)
      1 => string '3' (length=7)
      2 => string 'FBarbecue' (length=8)
      3 => string 'CBarbecue' (length=8)

How could I do to merge the corresponding items into a single array, perhaps using a foreach?

    
asked by anonymous 13.09.2017 / 13:29

2 answers

2

To create an array in the way you explained, simply use the array_map function by passing the first parameter to NULL , so an array is returned with the combinations of the input arrays. For example:

$tempero = ["Ketchup", "Mustard", "Barbecue"];
$quantidade = ["1", "2", "3"];
$frequencia = ["FKetchup", "FMustard", "FBarbecue"];
$combo = ["CKetchup", "CMustard", "CBarbecue"];

$saida = array_map(NULL, $tempero, $quantidade, $frequencia, $combo); 

Generate an array of the form:

Array
(
    [0] => Array
        (
            [0] => Ketchup
            [1] => 1
            [2] => FKetchup
            [3] => CKetchup
        )

    [1] => Array
        (
            [0] => Mustard
            [1] => 2
            [2] => FMustard
            [3] => CMustard
        )

    [2] => Array
        (
            [0] => Barbecue
            [1] => 3
            [2] => FBarbecue
            [3] => CBarbecue
        )

)
  

See working at Ideone .

If these arrays come from POST, just do something like:

$saida = array_map(NULL, $_POST["temperos"], $_POST["quantidade"], ...);
    
13.09.2017 / 14:06
0

Try using this code:

$temperos = array();
foreach($itens['tempero'] as $index => $item){
    $indice = 'tempero'.$index;
    $temperos[$indice] = array($item, $itens['quantidade'][$index], $itens['frequencia'][$index], $itens['combo'][$index]);
}
    
13.09.2017 / 13:57