1

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'.

2
  • What version of php are you looking for? 5.3? Commented Mar 11, 2010 at 0:11
  • yeah Im using the latest and greatest Commented Mar 11, 2010 at 0:11

1 Answer 1

1

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

2 Comments

Stunned!, Im really confused as to how its doing it though! Can you add some step by step commenting on it for me please? Thanks so much! and here I was expecting a one line answer!
I've added some comments in, but I had the javascript hammer in my hand when I saw your question pop by and gave you an overly complex answer for what you're trying to do. If your $courses array was indexed by the course_id instead of numerically, you could also do a one liner combining a few calls to the other native array functions, but the foreach loop is much more maintainable in the long run for its simplicity to read.

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.