Nasirsom Nasirsom - 4 months ago 12
PHP Question

Extract complete array from std class multi dimensional array php

I am working on an exam system and I have run into a problem for getting the right result.
I want this result from answers array which match question ID 466

(
[id] => 234
[firstChoice] => 2
[choice] => 2
[marked] =>
[strikethrough] => Array()
[highlights] =>
[guessed] =>
[difficulty] => easy
[numTimesChanged] =>
[timeElapsed] => 36
)


I have this type of answers std class array.
I have same type of array for questions too.

Array(
[0] => stdClass Object
(
[id] => 234
[firstChoice] => 2
[choice] => 2
[marked] =>
[strikethrough] => Array
(
)

[highlights] =>
[guessed] =>
[difficulty] => easy
[numTimesChanged] =>
[timeElapsed] => 36
)

[1] => stdClass Object
(
[id] => 466
[firstChoice] => 3
[choice] => 3
[marked] =>
[strikethrough] => Array
(
)

[highlights] =>
[guessed] =>
[difficulty] => easy
[numTimesChanged] =>
[timeElapsed] => 5
)
)

Answer

Try this:

$result = null;
foreach($array as $value){
    if($value->id == 466){
        $result = $value;
        break;
    }
}
Comments