I can insert a single record from variables into a custom WordPress table using the following method:
global $wpdb;
$wpdb->custom_table = $wpdb->prefix . 'clients';
$wpdb->query( $wpdb->prepare(
"
INSERT INTO $wpdb->custom_table
(client_id, client_name)
VALUES ( %s, %s )
",
array(
$client_id,
$client_name
)
) );
However, I need to import large numbers of records (manual and scheduled) via an API which provides me with an array named $clients with the following structure:
Array
(
[0] => Array
(
[id] => 1234567
[name] => Client 1
[other_field] => misc
)
[1] => Array
(
[id] => 1234568
[name] => Client 2
[other_field] => misc
)
[2] => Array
(
[id] => 1234569
[name] => Client 3
[other_field] => misc
)
)
I want to insert each [id] and [name] from the $clients array as a row in the table.
If the record exists based on the client_id column (Unique index) then I need to update the row in the table.