人才網履歷保密關鍵字:以半形逗號分隔字串儲存,顯示時轉陣列,需支援刪除指定關鍵字。
<?php
// 1. 從資料庫讀取逗號分隔字串
$sql = "SELECT key_secret FROM resume_relation WHERE userid = '$userid'";
$row = mysql_fetch_assoc(mysql_query($sql));
$keywords = explode(',', $row['key_secret']); // 轉陣列
// 2. 刪除指定索引 $autoid(從前端傳入)
array_splice($keywords, $autoid, 1); // 從 $autoid 起刪 1 個
// 3. 轉回字串存庫
$newSecret = implode(',', $keywords);
mysql_query("UPDATE resume_relation SET key_secret = '$newSecret' WHERE userid = '$userid'");
?>
array_splice(array &$input, int $offset, int|null $length = null, mixed $replacement = []): array
$offset:起始位置(負數從尾部算起)$length:刪除數量(省略則刪至結尾)$replacement:可選,替換插入的元素$arr = [1,2,3,4,5];
// 替換:索引 1~2 替換為 'a','b'
array_splice($arr, 1, 2, ['a','b']); // [1,'a','b',4,5]
// 僅插入不刪除(length=0)
array_splice($arr, 2, 0, ['x','y']); // 在索引 2 前插入
// 尾部追加
array_splice($arr, count($arr), 0, [99]);
SplFixedArray 或資料庫層處理mysql_* 函數已棄用,請改用 mysqli/PDO