1

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.

Jakeones
  • 39
  • 6
  • I don't know what you want but, you can use $studentsEditInfo['main'] as $key => $values in loop may helpful for you, – Viet Dinh Jun 16 '20 at 15:24
  • You're going to have to match them up by the order if you don't always know the key names. – AbraCadaver Jun 16 '20 at 15:37
  • 1
    Does this answer your question? [PHP append one array to another (not array\_push or +)](https://stackoverflow.com/questions/4268871/php-append-one-array-to-another-not-array-push-or) – fjsv Jun 16 '20 at 21:09
  • "How to append values from one array to another" in your title... "I don't know how to access the indices of my "total" array inside the foreach loop of my "main" array." What are you asking? To fetch a value or to merge 2 arrays? – bestprogrammerintheworld Jun 16 '20 at 23:07

2 Answers2

1

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.

Christoph Kern
  • 419
  • 2
  • 7
0
$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++;
        }
  • Please provide an explanation for your code and why it solves the problem. Posting plain code is not helpful for others. – Alex Jun 16 '20 at 16:45