Skip to content
Advertisement

how to combine PHP arrays from form entries

I have a form that allows the user to select multiple “keys” and then dynamically add any amount of values after. I need to combine them tied to each “key” to insert as rows in CSV. example data from form submit:

$locations = array(loc1,loc2,loc3,loc4)
$input1 = array(A1,A2,A3,A4,A5)
$input2 = array(B1,B2,B3,B4,B5)
$input3 = array(C1,C2,C3,C4,C5)

Output as:

$OutToCVS = array(loc1,A1,A2,A3,A4,A5)
$OutToCVS = array(loc1,B1,B2,B3,B4,B5)
$OutToCVS = array(loc1,C1,C2,C3,C4,C5)
$OutToCVS = array(loc2,A1,A2,A3,A4,A5)
$OutToCVS = array(loc2,B1,B2,B3,B4,B5)
$OutToCVS = array(loc2,C1,C2,C3,C4,C5)
$OutToCVS = array(loc3,A1,A2,A3,A4,A5)
$OutToCVS = array(loc3,B1,B2,B3,B4,B5)
$OutToCVS = array(loc3,C1,C2,C3,C4,C5)
$OutToCVS = array(loc4,A1,A2,A3,A4,A5)
$OutToCVS = array(loc4,B1,B2,B3,B4,B5)
$OutToCVS = array(loc4,C1,C2,C3,C4,C5)

Any help would be appreciated.

Advertisement

Answer

Example code to start with:

foreach ($locations as $loc) {
    $OutToCVS[] = array_merge([$loc], $input1);
    $OutToCVS[] = array_merge([$loc], $input2);
    $OutToCVS[] = array_merge([$loc], $input3);
}

Fiddle.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement