2 回答

TA貢獻1860條經驗 獲得超8個贊
所以,如果你說的是正確的,看起來你已經正確地過濾了你的,以排除那些有不需要的s.干得好!現在,您需要返回還包含分頁數據的 JSON。首先,讓我們逐步完成您的代碼,看看我們是如何在這里結束的:contentid
$response = ….get(url)………
$list = $response->getBody()[‘content’];
就在這里,在前兩行中,我們看到您僅從響應正文中選擇了 并將其存儲在 .content$list
$collection = collect($list);
$filtered = $collection->filter(function ($item) [
return auth()->user->permission($item['id'])
]);
現在,您正在獲取它并根據 進行過濾。contentid
Return [‘content’ => $filtered];
最后,您將返回已過濾的 .因此,根本沒有刪除任何內容 - 您剛剛提取了 ,對其進行了過濾,然后返回了它。您的數據仍然位于您離開的位置。為了獲得你想要返回的JSON,你可以調整你拉出和過濾它的方式,但是由于我不知道你正在做什么或為什么以及為什么以及你的過濾器已經在工作,我的直覺是為什么打擾?您可以獲取數據并將其與過濾后的數據相結合,然后您將獲得所需的確切內容:contentcontentpaginationcontentpaginationcontent
$response = ….get(url)………
$list = $response->getBody()[‘content’];
$pagination = $response->getBody()[‘pagination’];
$collection = collect($list);
$filtered = $collection->filter(function ($item) [
return auth()->user->permission($item['id'])
]);
$alldata = // combine your pagination and filtered objects
Return [‘data’ => $alldata];
請注意,我沒有展示如何組合和過濾 - 這是因為我不確定此時您擁有哪種類型的對象。由于您的示例輸出不是真正正確的JSON,我不確定是否真的應該是一個列表/數組,或者它是否只是您編寫示例的方式,我不確定這些是字符串還是它們是否已被解析,等等。因此,字符串需要將一個附加到另一個,集合將使用 ,依此類推。paginationcontentpaginationcombine()

TA貢獻1847條經驗 獲得超7個贊
您的數組是無效數組!所以我不能嘗試,正如我在評論中提到的!
我從閱讀您的所有編輯中了解到,這里有一些示例,您如何從php中的數組中刪除id 2:
在開始之前:我們可以選擇忽略php中的id 2,其中子句像這樣
SELECT * FROM table WHERE id NOT IN (2)
OR
SELECT FROM table WHERE id <> 2
您可以在查詢時忽略id 2,然后您將有一個沒有id 2的數組,讓我們與數組連接
如果要刪除數組中的特定值,請執行以下操作。
從關聯數組中刪除
$YourArray = array("id"=>"2","name"=>"somename");
//use array key exist to find if id exist
$test = array_key_exists("id",$YourArray);
if($test == 2){
unset($YourArray['id']); //Here you unset id 2
}
print_r($YourArray); //result of new array
使用多個數組的第二個解決方案:
// PHP program to creating two
// dimensional associative array
$data = array(
// Ankit will act as key
"paginate" => array(
// Subject and marks are
// the key value pair
"id" => 1,
"name" => "Widget #1",
"uri"=> "/products/1"
),
// Ram will act as key
"content" => array(
// Subject and marks are
// the key value pair
"id" => 2,
"name" => "Widget #1",
"uri"=> "/products/1"
),
);
echo "Display Marks: \n";
foreach ($data as $key => $subArr) {
if($subArr['id'] == 2){
unset($subArr['id']);
$data[$key] = $subArr;
}
}
print_r($data);
這是演示:https://3v4l.org/mvZPN
如果要刪除數組中的id及其所有元素:已使用array_filter();
$test = array_filter($data, function($data) { return $data['id'] != 2; });
print_r($test);
這是演示:https://3v4l.org/UgNqu
- 2 回答
- 0 關注
- 118 瀏覽
添加回答
舉報