I currently have a multidimensional array with two subarrays. The way the database is constructed gives me this result. what I want to do is create a new array with submission id and have the other values merged together. the only problem is that the values are the same keys but not the values.
(
[0] => Array
(
[sub_field_id] => 123
[submission_id] => 8
[field_id] => 81
[form_id] => 9
[value] => Male
)
[1] => Array
(
[sub_field_id] => 130
[submission_id] => 8
[field_id] => 92
[form_id] => 9
[value] => N4 Mar 27 theory Apr 20-23 onbike
)
)
I tried using foreach to create a new array
$this_submission = $sub_array['submission_id'];
$submission_keys[$this_submission][$result] = array('value'=> $sub_array['value']);
}
But the results don't help
[8] => Array
(
[0] => Array
(
[value] => Male
)
[1] => Array
(
[value] => N4 Mar 27 theory Apr 20-23 onbike
)
)
what I would like to get is the following:
[8] => Array
(
[0] => Array
(
[gender] => Male,
[date] => N4 Mar 27 theory Apr 20-23 onbike
)
)
Is this possible?
If you have all the arrays in a pattern like you show above this code works. working solution demo
$arr = [
[
"sub_field_id" => "123",
"submission_id" => "8",
"field_id" => "81",
"form_id" => "9",
"value" => "Male",
],
[
"sub_field_id" => "123",
"submission_id" => "8",
"field_id" => "81",
"form_id" => "9",
"value" => "N4 Mar 27 theory Apr 20-23 onbike",
]
];
$res_arr = [];
$arr_count = count($arr);
foreach($arr as $key=>$val){
if($key < $arr_count-1){
//here I am getting the last value from the current array and the next array.
//so if you know the key names you can get the value as $val = $arr[$key]['key_name'];
$end_val1 = end($arr[$key]);
$end_val2 = end($arr[$key+1]);
$temp = array_merge($arr[$key], $arr[$key+1]);
array_pop($temp);
array_values($temp);
$temp['gender'] = $end_val1;
$temp['date'] = $end_val2;
$res_arr[] = $temp;
}
}
print_r($res_arr);
Output:
Array
(
[0] => Array
(
[sub_field_id] => 123
[submission_id] => 8
[field_id] => 81
[form_id] => 9
[gender] => Male
[date] => N4 Mar 27 theory Apr 20-23 onbike
)
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.