Midhun
Midhun

Reputation: 115

how to get key of an array by using object in php

I have two arrays one is like-

Array
(
    [0] => Array
        (
            [student_id] => 2
            [attendance_type] => leave
        )

    [1] => Array
        (
            [student_id] => 3
            [attendance_type] => absent
        )

)

and other one is -

Array
(
    [0] => Array
        (
            [attendance_id] => 4
            [student_id] => 2
        )

)

From the first array I need to take attendance_type with correspond student_id, so I'm trying to pick key from the array first then attendacnce_type. How it possible??

Upvotes: 0

Views: 45

Answers (1)

Naren
Naren

Reputation: 53

<?php
$arrayFirst = [['student_id' => 2, 'attendance_type' => 'leave'], ['student_id' => 3, 'attendance_type' => 'absent']];
$arraySecond = ['attendance_id' => 4, 'student_id' => 2];

foreach ($arrayFirst as $key => $arrayFst) {
    if($arraySecond['student_id'] == $arrayFst['student_id']) {
        $firstArrayKeyValue = $key;
        $attendance_type  = $arrayFst['attendance_type'];
    }
}
echo $firstArrayKeyValue."<br>".$attendance_type;

?>

Upvotes: 2

Related Questions