1

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.

3
  • Just to confirm: your expected output here would be 2401 and 1308? Commented Nov 23, 2020 at 18:28
  • 1108 also exists in both. Commented Nov 23, 2020 at 18:34
  • yes, those three. I'll then have the course array check against 7 other arrays with similar coursa data (math, fine arts etc.) Commented Nov 23, 2020 at 19:42

3 Answers 3

1

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;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This is the one that worked best for me, and was easiest to adapt. Much appreciated!
Glad I could help
1

You're probalby looking for array_flip function, than it's quite simply.

<?php

$courses = [
    'BIOL-1108',
    'BIOL-1308'
];

$completed = [
    'course' => [
        [
            'course' => 'BIOL-1108',
            'title' => 'title',
            'grade' => 'C'
        ],
        [
            'course' => 'BIOL-1308',
            'title' => 'title',
            'grade' => 'C'
        ],
        [
            'course' => 'BIOL-1408',
            'title' => 'title',
            'grade' => 'C'
        ]
    ]
];


$courses = array_flip($courses);

foreach ($completed['course'] as $row) {
    if (!isset($courses[$row['course']])) {
        echo $row['course'] . '<BR>'; // returns BIOL-1408 in my example
    }
}

2 Comments

I'd need it to pull 2 of the three and not BIOL-1408 in this example. would isset instead of !isset be right instead
@trf: sorry, isset do the job :-)
1

You can loop through your array (assuming variable names) and use the in_array to check if the course title exists.

foreach($user['course'] as $course):
    if(in_array($course['title'], $completedCourses)):
        printf('Title: %s, Grade: %s', $course['title'], $course['grade']);
    endif;
endforeach;

1 Comment

Well apparently I have 3 answers that work. Thanks to all three of you. I've accepted the first answer even though all three technically do the job. but all get upvoted. I look forward to any comments that come in so I can learn a bit more.

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.