我已從表中選擇行
目標行是id等于的行$_GET['id']
我想將其移動到行的頂部
$sq = "select * from video order by ind asc";
$st = $db->prepare($sq);
$st->execute();
$rows = $st->fetchAll();
if(isset($_GET['id'])){
foreach($rows as $row){
if($row['id'] == $_GET['id']){
unset($rows[$row]); // error line
$rows = $row $rows;
}
}
}
錯誤 -Illegal offset type in unset...
還有 - 是否有更短的方法來做到這一點,即避免回圈
類似:
$rows[having id equal to $_GET['id']] -> move-to-top
uj5u.com熱心網友回復:
對于這條線,
unset($rows[$row]); // error line
您需要取消設定 key in$rows而不是$row本身,這是 foreach 回圈中的一個值。
因此,對于取消設定,它看起來像:
<?php
foreach($rows as $key => $row){
if($row['id'] == $_GET['id']){
unset($rows[$key]);
// code line to move to the top
break;
}
}
不要使用$rows = $row $rows;某種語法,因為它在代碼審查期間難以閱讀。
對于更短的語法,您可以使用array_filter過濾掉該行,然后借助symmetric-array-destructuring.
片段:
<?php
$row = array_filter($rows, fn($arr) => $arr['id'] == $testID);
[$rows[array_keys($row)[0]], $rows[0]] = [$rows[0], $rows[array_keys($row)[0]]];
在線演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/469956.html
上一篇:使用Malloc的2D數學矩陣
