Reputation: 3794
This is the array which i get in my form post form multiple check boxes.
$test1 = array(
'0' => 'test1',
'1' => 'test2',
'2' => 'test3'
);
$test2 = array(
'0' => 'test21',
'1' => 'test22',
'2' => 'test23'
);
$test3 = array(
'0' => 'test31',
'1' => 'test32',
'2' => 'test33'
);
$test4 = array(
'0' => 'test41',
'1' => 'test42',
'2' => 'test43'
);
I need to convert this array in to something like this:
$result_needed = [
'0' => ['0' => 'test1', '1' => 'test21', '2' => 'test31', '3' => 'test41'],
'1' => ['0' => 'test2', '1' => 'test22', '2' => 'test32', '3' => 'test42'],
AND SO ON....
];
I have tried to add this each array in to final array and then used foreach loop on it it get the result but it didn't help. Here is what i tried.
$final = ['test1' => $test1, 'test2' => $test2, 'test3' => $test3, 'test4' => $test4];
echo "<pre>";
$step1 = array();
foreach($final as $key => $val){
$step1[$key] = $val;
}
print_r($step1);
Upvotes: 2
Views: 75
Reputation: 569
If all the array has same length and all are index array.
$result = array();
foreach($test1 as $key=> $test){
$result[] = [$test1[$key],$test2[$key],$test3[$key],$test4[$key]];
}
print_r($result);
Upvotes: 1
Reputation: 1981
You can do it with loops and pushing to result array
$final = ['test1' => $test1, 'test2' => $test2, 'test3' => $test3, 'test4' => $test4];
$step1 = [];
foreach ($final as $tests) {
foreach ($tests as $key => $value) {
if (!array_key_exists($key, $step1)) {
$step1[$key] = [];
}
$step1[$key][] = $value;
}
}
print_r($step1);
Upvotes: 5