我有一個(gè)數(shù)組的數(shù)組。還有另一個(gè)數(shù)組的數(shù)組。如果存在一個(gè)具有 common 值的數(shù)組,我希望將其從第一個(gè)數(shù)組中刪除:這是我的代碼function availableTables($tables, $tablesWithDate) { $tablesReturn = array(); foreach($tables as $table) { foreach($tablesWithDate as $twd) { if($table['table_no'] == $twd['number']){ echo "true"; } else { echo "false"; } } } return $tablesReturn; }這樣,除了 2 次返回 true 之外,每次迭代的輸出都是 false。這是正確的,但我想從第一個(gè)數(shù)組中刪除它們。我努力了:function availableTables($tables, $tablesWithDate) { $tablesReturn = array(); foreach($tables as $table) { foreach($tablesWithDate as $twd) { if($table['table_no'] == $twd['number']){ unset($table); } else { echo "false"; } } } return $tablesReturn; }但不起作用。它說(shuō)未定義的變量表。我也嘗試過(guò)unset($tables[$table])$TablesWithDate:Array( [0] => Array ( [id] => 206 [number] => 150 [capacity] => 4 [booking_code] => qhJEHcWnzty062DD [reservation_date] => 2020-07-09 01:00:00 [start_time] => 12:30:00.000000 [end_time] => 14:15:00.000000 ) [1] => Array ( [id] => 206 [number] => 150 [capacity] => 4 [booking_code] => ym9dP1aZtFstP3WM [reservation_date] => 2020-07-22 01:00:00 [start_time] => 20:00:00.000000 [end_time] => 21:45:00.000000 ))表:( [0] => Array ( [id] => 159 [table_no] => 150 [capacity] => 6 [shape] => large_rectangle [joinable] => 1 [area] => 1 [baby_friendly] => 1 [premise_code] => LJJIDHhRN2ho1e3h [area_name] => Ferkin [premise_name] => An Poitin Stil )
1 回答

LEATH
TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊
使用表數(shù)組的索引(代碼中的$key)并使用 取消設(shè)置unset($tables[$key]。
function availableTables($tables, $tablesWithDate) {
$tablesReturn = array();
foreach($tables as $key => $table) {
foreach($tablesWithDate as $twd) {
if($table['table_no'] == $twd['number']){
unset($tables[$key]);
} else {
echo "false";
}
}
}
return $tables;
}
- 1 回答
- 0 關(guān)注
- 174 瀏覽
添加回答
舉報(bào)
0/150
提交
取消