2016-08-02 105 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