1

I have the following array $students:

[
(int) 0 => [
    'Students' => [
        'number' => '1364249',
        'first_name' => 'a',
        'last_name' => 'asda',
        'email' => '[email protected]'
    ],
    'Responses' => [
        'id' => '2'
    ]
  ]
]

There could me multiple students here. I have a variable before I get this array, which is a student number. I must then check this array to see if the student number exists anywhere. How would I do this? I tried the below but get an error 'Undefined index: number '.

$student_id = $this->Auth->user('Student.number');

    $authorised = false;
    foreach ($students as $student) {
        if (isset($student['Students'])) {
            if ($student['number'] == $student_id) {
                $authorised = true;
            }
        }
    }

I'm not good at PHP so I apologise if this is really obvious, I suspect I'm just doing the loop through the array very wrong, would appreciate any guidance.

1
  • Can you post the actual array without pre tag? Commented Feb 11, 2016 at 10:51

1 Answer 1

2

Try this:

foreach ($array as $student) {
     if (isset($student['Students'])) {
         if ($student['Students']['number'] == $student_id) {
              $authorised = true;
         }
     }
}
Sign up to request clarification or add additional context in comments.

1 Comment

$student['Students']['number'] is what I had trouble working out.. thanks!

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.