Gather background information that is in different arrays in the same array

Asked

Viewed 215 times

4

When the user of a submit in the form, I receive the following arrays (in this case I had 3 products in the form, if I had only 1, in the 4 arrays I would have only the Indice 0, if I had 5 products, in the 4 arrays I would have indices 0, 1, 2, 3 and 4)

'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 this, I would need to join 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 join the corresponding items into a single array, perhaps using a foreach?

2 answers

2


To create a array as explained, just use the function array_map passing the first parameter as NULL, so it will be returned one array with the combinations of arrays input. 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); 

Will generate a array in 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 on Ideone.

If these arrays comes from the POST, just do something like:

$saida = array_map(NULL, $_POST["temperos"], $_POST["quantidade"], ...);
  • Ball show, that’s exactly what I was looking for. I had tried with various forms of foreach, but in the end you didn’t have to use.

0

Try to use 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]);
}

Browser other questions tagged

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