1

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?

4 Answers 4

6

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);
Sign up to request clarification or add additional context in comments.

Comments

2

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);

Comments

1

@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);

1 Comment

Vamsi Krishna beat me to it
0

You need convert to dimensional second array and after use array_diff_key()

Comments

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.