I'm using multi-dimensional input tags in my html:
Car 1
<input name="warehouse[cars][][name]" />
<input name="warehouse[cars][][model]" />
<input name="warehouse[cars][][year]" />
Car 2
<input name="warehouse[cars][][name]" />
<input name="warehouse[cars][][model]" />
<input name="warehouse[cars][][year]" />
The issue with that is that i'm getting some messed up array in the PHP side:
Array:warehouse->cars
(
[0] => Array
(
[name] => Audi,
[name] => BMW
)
[1] => Array
(
[model] => S4,
[model] => X5
)
[2] => Array
(
[year] => 2010
[year] => 2011
)
)
As you see it's all separated instead of something like:
$warehouse['cars'] = array(
[0] => array(
'name' => 'Audi',
'model' => 'S4',
'year' => '2010'
)
....
)
How to fix/re-group this kind of array inputs?
P.S. i know i can do warehouse[cars][number here][name] but i prefer not to.
I suggest you reduce the complexity of the arrays, so your code will be easy to debug later. Instead of warehouse[cars][][name] and such you can use cars_names[], cars_models[] and so on.
So when you read the values you can use smthing like this
$j=0;
if(count($_POST['cars_names'])) foreach($_POST['cars_names'] as $car){
$name = $car;
$model= $_POST['cars_models'][$j];
...
$j++;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With