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?
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);
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);
@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);
您需要转换为第二维数组,并在使用array_diff_key()之后
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.