从 std class 多维数组 php 中提取完整数组
Extract complete array from std class multi dimensional array php
我正在研究考试系统,但我 运行 遇到了获得正确结果的问题。
我想要这个结果来自匹配问题 ID 466
的答案数组
(
[id] => 234
[firstChoice] => 2
[choice] => 2
[marked] =>
[strikethrough] => Array()
[highlights] =>
[guessed] =>
[difficulty] => easy
[numTimesChanged] =>
[timeElapsed] => 36
)
我有这种类型的答案标准 class 数组。
我也有相同类型的问题数组。
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不唯一的情况下,可以使用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);
我正在研究考试系统,但我 运行 遇到了获得正确结果的问题。 我想要这个结果来自匹配问题 ID 466
的答案数组(
[id] => 234
[firstChoice] => 2
[choice] => 2
[marked] =>
[strikethrough] => Array()
[highlights] =>
[guessed] =>
[difficulty] => easy
[numTimesChanged] =>
[timeElapsed] => 36
)
我有这种类型的答案标准 class 数组。 我也有相同类型的问题数组。
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不唯一的情况下,可以使用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);