Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 650619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:04:23+00:00 2026-05-13T22:04:23+00:00

I have two arrays: $course = [ 6 => 24, 0 => 20, 1

  • 0

I have two arrays:

$course = [
    6 => 24,
    0 => 20,
    1 => 14,
    // etc...
];

And

[
    ['course_id' => 1, 'course_name' => 'Appetizer'],
    ['course_id' => 2, 'course_name' => 'Breakfast'],
    // etc
];

I want to merge related data between the two arrays based the keys in the $course array correlating to the course_id of the second array.

I want to be able to print out, for example, '1', 'Appetizer', '14'.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:04:23+00:00Added an answer on May 13, 2026 at 10:04 pm

    5.3’s closures combined with the various native array iteration functions make this in to a pretty painless process:

    <?php
    
    // where $key is a course_id and $value is the associated course's next course
    $nextCourse = array(6 => 24, 0 => 20, 1 => 14);
    
    // course definitions
    $courses = array(
            0 => array("course_id" => 1, "course_name" => 'Appetizer'),
            1 => array("course_id" => 2, "course_name" => 'Breakfast')
    );
    
    // loop through and add a next_course reference if one exists in $nextCourse map
    foreach($courses as &$course) {
            if (isset($nextCourse[$course["course_id"]])) {
                    $course["next_course"] = $nextCourse[$course["course_id"]];
            }
    }
    // $courses is now the same as the var dump at the end
    
    /**
     * A bit of 5.3 proselytism with native array functions and closures, which is
     * an overly complex answer for this particular question, but a powerful pattern. 
     *
     * Here we're going to iterate through the courses array by reference and 
     * make use of a closure to add the next course value, if it exists
     * 
     * array_walk iterates over the $courses array, passing each entry in
     * the array as $course into the enclosed function.  The parameter is
     * marked as pass-by-reference, so you'll get the modifiable array instead
     * of just a copy (a la pass-by-value).  The enclosure function requires
     * the 'use' keyword for bringing external variables in to scope.
     */
    array_walk($courses, function(&$course) use ($nextCourse) {
            /**
             * We check to see if the current $course's course_id is a key in the 
             * nextCourse link mapping, and if it is we take the link mapping
             * value and store it with $course as its next_course
             */
            if (isset($nextCourse[$course["course_id"]])) {
                    $course["next_course"] = $nextCourse[$course["course_id"]];
            }
    });
    
    var_dump($courses);
    
    /**
    Output:
    
    array(2) {
      [0]=>
      array(3) {
        ["course_id"]=>
        int(1)
        ["course_name"]=>
        string(9) "Appetizer"
        ["next_course"]=>
        int(14)
      }
      [1]=>
      array(2) {
        ["course_id"]=>
        int(2)
        ["course_name"]=>
        string(9) "Breakfast"
      }
    }
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two arrays of data that I would like to display in an
I have two arrays: $A = array('a','b','c','d') $c = array('b','c','e','f') I want to get
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two arrays. One contains id=>count and the other contains id=>name . I'm
I have two arrays containing the same elements, but in different orders, and I
I have two arrays in PHP. The first array ($author_array) is comprised of user_ids
I have two arrays built while parsing a text file. The first contains the
Let's say I have two arrays: int ArrayA[] = {5, 17, 150, 230, 285};
I'm using .NET 3.5. I have two string arrays, which may share one or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.