再会!我有一个关于从对象(Php Laravel)中的数组中检索特定对象的问题。比如说
这是我要检索的数据:
$variable = [array];
// This is the result whenever I use this on blade to see the result of the variable.
json_encode($variable, true);
//result
[
{"0":{
"id":1,
"name":"Joe",
"year":2012,
},
},
{"0":
{
"id":2,
"name":"Mark",
"year" : 2020,
}
},
{"0":
{
"id":2,
"name":"Mark",
"year" : 2020,
}
}
]
我想使用 php 检索具有 year = 2020 和 id = 2 的项目并将其插入到新变量中。
所以我期望结果是:
[
{
"0":
{
"id":2,
"name":"Mark",
"year" : 2020,
}
},
{"0":
{
"id":2,
"name":"Mark",
"year" : 2020,
}
}
]
我试过了 :
$new_val = []
foreach($variable as $key => value) {
if($value['year'] == "2020" && $value['id'] == "2") {
array_push($new_val , $variable[$key]):
}
}
感谢您阅读我的问题