2016-08-02 81 views
-2

我正在考试系统上工作,我遇到了一个问题以获得正确的结果。 我想从答案阵列这个结果是匹配问题ID 466从std类多维数组中提取完整的数组php

(
[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 
    ) 
) 
+1

什么都有你_tri ed_? – bwoebi

+0

如果可能的话,我会建议将ID设置为数据生成的关键字。 –

回答

0

试试这个:

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

在你的ID的情况并非个例,你可以使用array_filter()

解决方案:

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

Live example