从std类多维数组php中提取完整数组

从std类多维数组php中提取完整数组,php,multidimensional-array,stdclass,Php,Multidimensional Array,Stdclass,我正在做一个考试系统,为了得到正确的结果,我遇到了一个问题。 我希望这个结果来自与问题ID 466匹配的答案数组 ( [id] => 234 [firstChoice] => 2 [choice] => 2 [marked] => [strikethrough] => Array() [highlights] => [guessed] => [difficulty] => easy [numTimesChanged] => [time

我正在做一个考试系统,为了得到正确的结果,我遇到了一个问题。 我希望这个结果来自与问题ID 466匹配的答案数组

(
[id] => 234
[firstChoice] => 2
[choice] => 2
[marked] => 
[strikethrough] => Array()
[highlights] => 
[guessed] => 
[difficulty] => easy
[numTimesChanged] => 
[timeElapsed] => 36
)
我有这种类型的std类数组。 我也有同样类型的问题数组

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
    )
)
试试这个:

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

如果您的ID不唯一,您可以使用

解决方案:

<?php

$array = json_decode('[{"id":4,"data":"data1"},{"id":14,"data":"data41"},{"id":14,"data":"data14"}]');

$idSearched = 14;

function filter($item){
    global $idSearched;
    return $item->id === $idSearched;
}

$res = array_filter($array, "filter");
print_r($res);

您尝试过什么?如果可能,我建议在数据生成时将id设置为密钥。