[英]How do i get data based on the id in the array php
如何根據數組中的 id 獲取數據
array:60[
3 => array:3[
"id" => 4
"name" => james
"sex" => male
],
3 => array:3[
"id" => 5
"name" => mulan
"sex" => female
]
]
我使用 laravel,我使用如下代碼,但過濾的是不基於 id 的索引數組
$response = Curl::to('127.0.0.2:8000/user/data')->get();
$data = json_decode($response, true);
$outputData = $data["data"];
dd($outputData[$request->id]);
您可以將該數組放入 Collection 並使用where
方法根據id
返回項目:
$collection = collect($thatArray);
$item = $collection->where('id', 5);
您還可以通過 'id' 鍵入集合,然后可以使用get
:
$collection = collect($thatArray)->keyBy('id');
$item = $collection->get(5);
Laravel 6.x 文檔 - Collections - 可用方法 - where
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.