1 回答

TA貢獻1786條經驗 獲得超11個贊
$names您可以從您的和 中創建一個關聯數組,$data如下所示:
$count = sizeof($names); // Get the number of elements in the arrays
// for each element, add the name as the key and the data as the value:
for($i=0; $i<$count; $i++)
$associative_array[$names[$i]] = $data[$i];
這將創建一個像這樣的數組:
Array
(
[eleicons] => icons data
[elepro] => pro data
[elefont] => font data
[eleanimations] => animations data
)
您可以在這里看到輸出(當然使用虛擬數據):https ://sandbox.onlinephpfunctions.com/code/68c5f0a92b33625c437e4c5b9ef17c6f9b2ec4bb
或者,當您使用 時$_POST,您可以這樣創建數組:
foreach($names as $key)
$associative_array[$key] = $_POST[$key];
整個代碼將是:
$table = 'name';
$names = array('eleicons', 'elepro', 'elefont', 'eleanimations');
$associative_array = array();
foreach($names as $key)
$associative_array[$key] = $_POST[$key];
update($table,
$associative_array,
array('id' => 0));
重要的提示:
您正在代碼中使用tableand ,因此如果您要將其添加到數據庫中,則應該在將其添加到數據庫之前清理所有用戶提供的數據,否則您很容易受到 SQL 注入的影響。update
- 1 回答
- 0 關注
- 140 瀏覽
添加回答
舉報