I have two arrays with the following structure
array
'main' => array
'firstYearStudents' => array
0 => '10'
1 => '12'
'secondYearStudents' => array
0 => '8'
1 => '9'
'programCode' => array
0 => '03.02.01'
1 => '03.01.01'
'educationProgramName' => array
0 => 'Mathematics'
1 => 'Physics'
'total' => array
'totalFirstYear' => '22'
'totalSecondYear' => '17'
'programCode' => '-'
'totalEducationProgramName' => 'Total Directions'
Then the required structure should look something like this
array
'main' => array
'firstYearStudents' => array
0 => '10'
1 => '12'
2 => '22'
'secondYearStudents' => array
0 => '8'
1 => '9'
2 => '17'
'programCode' => array
0 => '03.02.01'
1 => '03.01.01'
2 => '-'
'educationProgramName' => array
0 => 'Mathematics'
1 => 'Physics'
2 => 'Total Directions'
I've tried the following but I got the named keys so I can't access these keys.
$i = 0;
foreach ($studentsEditInfo['main'] as $values) {
$studentsEditInfo['main'] = array_merge($values, $studentsEditInfo['total'][$i]);
$i++;
}
I don't know how to access the indices of my "total" array inside the foreach loop of my "main" array.
If the order of your "total"-Array always stays like this you could transform the total into an numeric array before you add the values.
Like this
$arr["total" ] = ["a" => 1,"b" => 2, "c" => 3];
$arrNumeric = [];
foreach ($arr["total"] as $item) {
$arrNumeric []= $item;
}
Afterwards you just need to run this code to add the array-values
$i = 0;
foreach ($studentsEditInfo['main'] as $values) {
$values []= $arrNumeric[$i];
$i++;
}
I did not test this, btw.
$total = [];
foreach ($studentsEditInfo['main']['total'] as $keyTotal => $valueTotal) {
$total[] = $valueTotal;
}
unset($studentsEditInfo['main']['total']);
$index = 0;
foreach ($studentsEditInfo['main'] as $keyMain => $valueMain) {
$studentsEditInfo['main'][$keyMain][] = $total[$index];
if ($index > count($studentsEditInfo['main'])) {
continue;
}
$index++;
}
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.