使用由 for 或 foreach 循环完成的关联数组创建一个 wordpress 更新查询
Create a wordpress update query with an associative array done by a for or foreach loop
我想通过循环创建关联数组以将其添加到更新中。我遇到的问题是我不知道如何遍历 2 个数组的每个元素来创建关联数组以放入更新查询。
你可以在这里看到我在更新查询中将数组放在另一个数组中,但这是错误的。
我有以下例子:
$table = 'name';
$data = [$_POST['eleicons'], $_POST['elepro'], $_POST['elefont'], $_POST['eleanimations']];
$names = ['eleicons', 'elepro', 'elefont', 'eleanimations'];
$counter = 0;
update($table,
array(
foreach($name as $one => $dat){
$one => $dat[$counter],
$counter++;
}),
array('id' => 0));
正如我所说,数组是关联的。更新函数应该是这样的:
update($table,
array(
$name[0] => $data[0],
$name[1] => $data[1],
$name[2] => $data[2],
$name[3] => $data[3],
),
array('id' => 0));
但我想这样做:
update($table,
$array_associative,
array('id' => 0));
您可以从 $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));
重要提示:
您在代码中使用了 table
和 update
,因此如果您将其添加到数据库中,您应该在将任何 user-provided 数据添加到数据库之前对其进行清理,否则你对 SQL 注入持开放态度。
我想通过循环创建关联数组以将其添加到更新中。我遇到的问题是我不知道如何遍历 2 个数组的每个元素来创建关联数组以放入更新查询。
你可以在这里看到我在更新查询中将数组放在另一个数组中,但这是错误的。
我有以下例子:
$table = 'name';
$data = [$_POST['eleicons'], $_POST['elepro'], $_POST['elefont'], $_POST['eleanimations']];
$names = ['eleicons', 'elepro', 'elefont', 'eleanimations'];
$counter = 0;
update($table,
array(
foreach($name as $one => $dat){
$one => $dat[$counter],
$counter++;
}),
array('id' => 0));
正如我所说,数组是关联的。更新函数应该是这样的:
update($table,
array(
$name[0] => $data[0],
$name[1] => $data[1],
$name[2] => $data[2],
$name[3] => $data[3],
),
array('id' => 0));
但我想这样做:
update($table,
$array_associative,
array('id' => 0));
您可以从 $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));
重要提示:
您在代码中使用了 table
和 update
,因此如果您将其添加到数据库中,您应该在将任何 user-provided 数据添加到数据库之前对其进行清理,否则你对 SQL 注入持开放态度。