我在Laravel應(yīng)用程序上工作,因此我有一個(gè)關(guān)聯(lián)數(shù)組要傳遞給API端點(diǎn)。在發(fā)布到API之前,我想刪除img鍵及其值。我嘗試使用未設(shè)置的功能,但未刪除img鍵我要?jiǎng)h除圖像屬性的數(shù)組 $a[] = [ 0 => array:4 [ "name" => "Martoo nnn" "relationship" => "Spouse" "dob" => "2001-02-03" "img" => "img.png" ] 1 => array:4 [ "name" => "sdsdsd sdsdsd" "relationship" => "Child" "dob" => "2019-04-04" "img" => "img1.png" ] 2 => array:4 [ "name" => "sdsdsd sddds" "relationship" => "Child" "dob" => "2019-04-05" "img" => "img2.png" ] 3 => array:4 [ "name" => "dssdsd dsdsd" "relationship" => "Child" "dob" => "2019-04-02" "img" => "img3.png" ] 4 => array:4 [ "name" => "dssdsd dssdsd" "relationship" => "Child" "dob" => "2019-04-04" "img" => "img4.png" ]];取消設(shè)定方法$array = $a;unset($array['img']);//dd($a);
2 回答

寶慕林4294392
TA貢獻(xiàn)2021條經(jīng)驗(yàn) 獲得超8個(gè)贊
你可以做這樣的事情,
foreach ($array as $key => &$value) { // & defines changes will be made @ value itself
unset($value['img']);
}
是的,我不明白您為什么要初始化$a為$a[]?

人到中年有點(diǎn)甜
TA貢獻(xiàn)1895條經(jīng)驗(yàn) 獲得超7個(gè)贊
$newarray = array_filter($a, function($k) {
return $k != 'img';
}, ARRAY_FILTER_USE_KEY);
并傳遞這個(gè)新數(shù)組
- 2 回答
- 0 關(guān)注
- 194 瀏覽
添加回答
舉報(bào)
0/150
提交
取消