Skip to content
Advertisement

Getting the values of a multidimensional array where key value is in another array

I have an array of courses:

 Array
        (
           [0] => BIOL-1108
           [1] => BIOL-1308
           [2] => BIOL-2401
           [3] => BIOL-2402
       )

And a multidimensional array of completed courses that looks like this:

Array
(
    [course] => Array
        (
            [0] => Array
                (
                    [course] => BIOL-2401
                    [title] => BIOL-2401 - Human Anatomy & Physiology I
                    [grade] => A
                )

            [1] => Array
                (
                    [course] => HIST-1301
                    [title] => HIST-1301 - History of the U.S. I
                    [grade] => B
                )

            [2] => Array
                (
                    [course] => MATH-0303
                    [title] => MATH-0303 - Intermediate Algebra
                    [grade] => F
                )
             [3] => Array
                (
                    [course] => BIOL-1108
                    [title] => BIOL-1108 - Life Science I Lab
                    [grade] => B
                )

            [4] => Array
                (
                    [course] => BIOL-1308
                    [title] => BIOL-1308 - Life Science I
                    [grade] => C
                )

       )
)

I want to echo only the course, title and grade if it exists in the first array. I think I am needing a foreach loop, but I’m stuck.

Advertisement

Answer

You are on the right track in that you need to loop over your multi-dimensional array values and then see if the courses are in your course array. This can be done using a foreach in conjunction with the in_array function.

<?php
        
$courses = array('BIOL-1108', 'BIOL-1308', 'BIOL-2401','BIOL-2402');

$completed_courses = array(
    'course' => array(
                array(
                    'course' => 'BIOL-2401',
                    'title' => 'BIOL-2401 - Human Anatomy & Physiology I',
                    'grade' => 'A'
                ),
                array(
                    'course' => 'HIST-1301',
                    'title' => 'HIST-1301 - History of the U.S. I',
                    'grade' => 'B'
                ),
                array
                (
                    'course' => 'MATH-0303',
                    'title' => 'MATH-0303 - Intermediate Algebra',
                    'grade' => 'F'
                ),
                array
                (
                    'course' => 'BIOL-1108',
                    'title' => 'BIOL-1108 - Life Science I Lab',
                    'grade' => 'B'
                ),
                array
                (
                    'course' => 'BIOL-1308',
                    'title' => 'BIOL-1308 - Life Science I',
                    'grade' => 'C'
                )

       )

    );

  

foreach( $completed_courses['course'] as $curr ){
    if(in_array($curr['course'], $courses)) {
        echo 'COURSE : ' . $curr['course'] . PHP_EOL;
        echo 'TITLE : ' .  $curr['title'] . PHP_EOL;
        echo 'GRADE : ' .  $curr['grade'] . PHP_EOL;
    }
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement