简体   繁体   中英

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??

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

?>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM