0

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

4
  • try using array_keys() function. (php.net/array_keys) Commented Jul 20, 2017 at 7:50
  • what is your desired output? Commented Jul 20, 2017 at 7:57
  • You have to loop into arrays and compare the desired key Commented Jul 20, 2017 at 7:58
  • @AlivetoDie I student_id from the second array and I want to pick attendance_type from the first array with respond with student_id Commented Jul 20, 2017 at 7:59

1 Answer 1

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;

?>
Sign up to request clarification or add additional context in comments.

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.