如何使用數組鍵保存陣列中的特定項目。保存具有特定鍵的項目陣列
輸入:
$arr = [
0 => 'item0',
2 => 'item2',
4 => 'item4',
43 => 'item43'
];
現在我只希望保存鍵和。
預期輸出:
$arr = [
2 => 'item2',
43 => 'item43'
];
目前代碼:
foreach ($arr as $key => $value) {
if(!($key == 2 || $key == 43)) {
unset($arr[$key]);
}
}
它適用於現在,但如果我有更多的數組鍵進行保存。
查看[array_key_exists](http://php.net/manual/en/function.arr ay-key-exists.php) – Epodax