如何在php中使用对象获取数组的键

时间:2017-07-20 07:49:23

标签: php arrays

我有两个数组,就像 -

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

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

)

和另一个是 -

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

)

从第一个数组开始,我需要将attendance_typestudent_id对齐,所以我首先尝试从数组中选择键attendacnce_type。怎么可能??

1 个答案:

答案 0 :(得分:2)

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

?>