Ny 数组看起来像这样:Illuminate\Database\Eloquent\Collection {#2052 ▼ #items: array:3 [▼ 0 => App\Models\NewsMeta {#2089 ▶} 1 => App\Models\NewsMeta {#2090 ▶} 2 => App\Models\NewsMeta {#2091 ▶} ]} 如果我打开数组2: #original: array:7 [▼ "id" => 17 "post_id" => 240231 "news_tag_id" => 5 "meta_name" => "_thumbnail_id" "meta_value" => "240232" "created_at" => "2020-08-06 22:34:06" "updated_at" => "2020-08-06 22:34:06" ]现在,鉴于我有 240231,我希望获得值“240232”。如何在对象的数组内部搜索?类似于:其中 post_id 是 240231 获取 ts meta_value。仅供参考:这不是雄辩的或数据库查询。
1 回答
偶然的你
TA贡献1841条经验 获得超3个赞
像这样的东西应该有效:
$postId = 240231;
$metaValue = null;
foreach ($collection as $model) {
if ($model->post_id == $postId) {
$metaValue = $model->meta_value;
break;
}
}
echo $metaValue;
您还可以使用集合的search方法:
$postId = 240231;
$metaValue = $collection->search(static function($model, $key) use($postId) {
if ($model->post_id == $postId) {
return $model->meta_value;
}
});
echo $metaValue;
- 1 回答
- 0 关注
- 73 浏览
添加回答
举报
0/150
提交
取消