First Array是
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
)
我的第二个数组是
Array
(
[0] => Array
(
[course_id] => 2
)
[1] => Array
(
[course_id] => 4
)
[2] => Array
(
[course_id] => 6
)
[3] => Array
(
[course_id] => 10
)
)
我的预期结果是
Array
(
[2] => Course 1
[4] => COMPUTATIONAL MATHEMATICS -I
[6] => DATA STRUCTURE
[10] => GROUP A:ENGINEERING DRAWING & WORKSHOP
)
我想知道第一个数组是否包含第二个数组中的值并返回结果数组。有什么想法吗?
答案 0 :(得分:6)
试试这个
$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);
答案 1 :(得分:2)
你可以这样做。将result
定义为数组。并使用for循环将第二个数组的值放入其中。
$result = array();
foreach($second_array as $sa){
$result[$sa] = $first_array[$sa];
}
print_r($result);
答案 2 :(得分:1)
@ Ronak的回答是正确的想法......结果的关键只需要引用course_id,否则会发生未定义的偏移错误。
很抱歉,我会评论但尚未足够的代表:)
$result = array();
foreach($second_array as $sa){
$result[$sa['course_id']] = $first_array[$sa['course_id']];
}
print_r($result);
答案 3 :(得分:0)
您需要转换为维度第二个数组并在使用array_diff_key()
之后