Reputation: 1132
First Array is
Array
(
[2] => Course 1
[3] => C2
[4] => COMPUTATIONAL MATHEMATICS -I
[5] => BASIC ELECTRONICS
[6] => DATA STRUCTURE
[7] => COMMUNICATION SKILL
[8] => SYSTEMS PROGRAMMING
[9] => DIGITAL LOGIC
[10] => GROUP A:ENGINEERING DRAWING & WORKSHOP
)
my second array is
Array
(
[0] => Array
(
[course_id] => 2
)
[1] => Array
(
[course_id] => 4
)
[2] => Array
(
[course_id] => 6
)
[3] => Array
(
[course_id] => 10
)
)
my expected result is
Array
(
[2] => Course 1
[4] => COMPUTATIONAL MATHEMATICS -I
[6] => DATA STRUCTURE
[10] => GROUP A:ENGINEERING DRAWING & WORKSHOP
)
I would like to find out if first array contains value in second array and return the result array . any idea please?
Upvotes: 1
Views: 89
Reputation: 283
@Ronak's answer is the correct idea...the keys for the result just need to reference the course_id, otherwise an undefined offset error occurs.
Sorry, I would have commented but not enough rep yet :)
$result = array();
foreach($second_array as $sa){
$result[$sa['course_id']] = $first_array[$sa['course_id']];
}
print_r($result);
Upvotes: 1
Reputation: 161
Try this
$firstArray = Array();
$firstArray[2] = "Course 1";
$firstArray[3] = "C2";
$firstArray[4] = "COMPUTATIONAL MATHEMATICS -I";
$firstArray[5] = "BASIC ELECTRONICS";
$firstArray[6] = "DATA STRUCTURE";
$firstArray[7] = "COMMUNICATION SKILL";
$firstArray[8] = "SYSTEMS PROGRAMMING";
$firstArray[9] = "DIGITAL LOGIC";
$firstArray[10] = "GROUP A:ENGINEERING DRAWING & WORKSHOP";
$secondArray = Array();
$secondArray[0]["course_id"] = 2;
$secondArray[1]["course_id"] = 4;
$secondArray[2]["course_id"] = 6;
$secondArray[3]["course_id"] = 10;
$result = array();
foreach($secondArray as $second){
$result[$second["course_id"]] = $firstArray[$second["course_id"]];
}
echo '<pre>';
print_r($result);
Upvotes: 6
Reputation: 739
You need convert to dimensional second array and after use array_diff_key()
Upvotes: 0
Reputation: 5792
You can do something like this. Define result
as array. and put second array's values in it by using for loop.
$result = array();
foreach($second_array as $sa){
$result[$sa] = $first_array[$sa];
}
print_r($result);
Upvotes: 2