So, I am sending data from View to Controller and need to save all (multiple) data in DB.
in view I am using category[]
, status_number[]
and name[]
to collect data, then I got this:
[
'category' => [
(int) 0 => 'c',
(int) 1 => 'c1',
(int) 2 => 'c2'
],
'status_number' => [
(int) 0 => 'sn',
(int) 1 => 'sn1',
(int) 2 => 'sn2'
],
'name' => [
(int) 0 => 'n',
(int) 1 => 'n1',
(int) 2 => 'n2'
]
]
now I need to transform (combine) this array so that I can store it to DB in this structure
[
'(int) 0' => [
category => 'c',
status_number => 'sn',
name => 'n'
],
'(int) 1' => [
category => 'c1',
status_number => 'sn1',
name => 'n1'
],
'(int) 2' => [
category => 'c2',
status_number => 'sn2',
name => 'n2'
]
]
You can try something like that:
<?php
$array = [
'category' => [
'c',
'c1',
'c2'
],
'status_number' => [
'sn',
'sn1',
'sn2'
],
'name' => [
'n',
'n1',
'n2'
]
];
$result = [];
array_walk($array, function($data, $key) use (&$result){
foreach($data as $numericKey => $value) {
$result[$numericKey][$key] = $value;
}
});
print_r($result);
As the result it gives:
Array
(
[0] => Array
(
[category] => c
[status_number] => sn
[name] => n
)
[1] => Array
(
[category] => c1
[status_number] => sn1
[name] => n1
)
[2] => Array
(
[category] => c2
[status_number] => sn2
[name] => n2
)
)