Search code examples
phparraysformsjoindynamic-data

How to join 3 variables from 3 arrays with the same labels into a fourth array in php


i have 3 arrays that i get from a dynamically generated form(there could be a variable number of entries but the number of entries for each of these 3 arrays will always be the same):

services[0] => $service1
services[1] => $service2
services[2] => $service3

amount[0] => $amount1
amount[1] => $amount2
amount[2] => $amount3

price[0] => $price1
price[1] => $price2
price[2] => $price3

How do i make an array for each variable of those arrays? For each service with the same label i want to make an array called service like this:

Service1[0] => services[0]
Service1[1] => amount[0]
Service1[2] => price[0]

Service2[0] => services[1]
Service2[1] => amount[1]
Service2[2] => price[1]

Service3[0] => services[2]
Service3[1] => amount[2]
Service3[2] => price[2]

And eventually make a final array like this:

Services[0] => Service1
Services[1] => Service2
Services[2] => Service3

I could easily do it if i knew the exact number of variables i get from the form but since it's dinamycally generated i don't know that. Thanks


Solution

  • for($i=0;$i<3;$i++){
      $Services[$i]=array($services[$i],$amount[$i],$price[$i]);
    }
    

    In php you need $ before variables.