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.

4
  • I don't know what you want but, you can use $studentsEditInfo['main'] as $key => $values in loop may helpful for you, Commented Jun 16, 2020 at 15:24
  • You're going to have to match them up by the order if you don't always know the key names. Commented Jun 16, 2020 at 15:37
  • 1
    Does this answer your question? PHP append one array to another (not array_push or +) Commented Jun 16, 2020 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? Commented Jun 16, 2020 at 23:07

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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++;
        }

1 Comment

Please provide an explanation for your code and why it solves the problem. Posting plain code is not helpful for others.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.