So far the answers I have found is all about one associative array one indexed array. I got two associative arrays like the below:
$updates = array (
array ( 'value' => $check_first ),
array ( 'value' => $check_second )
);
$conditions = array (
array ( 'name' => 'enable' ),
array ( 'name' => 'picked' )
);
I wanna use foreach to update the wpdb database like below but it doesn't work, yeah I know foreach won't work like this, any other solution?
foreach ( $updates as $names_1 => $update ) {
foreach( $conditions as $names_2 => $condition ) {
$wpdb -> update ( $table_name, $update, $condition );
}
}
CodePudding user response:
Condition arrays' values can be arrays themselves and you can pass an associative array as a condition directly to $wpdb->update()
. If you want every update to have its own conditions:
Array:
$updates = [
[
'fields' => [
'field1' => 'value1',
'field2' => 'value2',
],
'conditions' => [
'name' => ['enable', 'picked'],
],
],
];
Loop:
foreach ($updates as $update) {
$wpdb->update($table_name, $update['fields'], $update['conditions']);
}
This is equivalent to the following SQL query:
UPDATE $table_name
SET field1 = 'value1', field2 = 'value2'
WHERE name IN ('enable', 'picked');