1

I have the following which works for the first array but doesn't work for the second. How can I combine the two and use sendinblue_runner($data) once?

    // Fire the function to add to the imp_customer_log table
    $data = array(
        'id'                            => '',
        'customer_id'           => $data['user_id'],
        'event'                     => 'first_name',
        'data'                      => $data['account_first_name'],
        'updated_in_sib'    => '0',
    );
    sendinblue_runner($data);

    // Fire the function to add to the imp_customer_log table
    $data = array(
        'id'                            => '',
        'customer_id'           => $data['user_id'],
        'event'                     => 'last_name',
        'data'                      => $data['account_last_name'],
        'updated_in_sib'    => '0',
    );
    sendinblue_runner($data);

And then I have my runner:

// Runner to grab data from all the functions that affect SiB contact fields
function sendinblue_runner($data) {
  global $wpdb;
  $table = "imp_customer_log";
  $wpdb->insert( $table, $data );
}
KIKO Software
  • 15,283
  • 3
  • 18
  • 33
Rob
  • 6,304
  • 24
  • 83
  • 189
  • You can use [array_merge()](https://www.php.net/manual/en/function.array-merge.php) to merge both arrays – ReynierPM Aug 15 '19 at 15:46
  • @ReynierPM I tried `array_merge` but it just output the second array – Rob Aug 15 '19 at 15:48
  • From the docs: "If the input arrays have the same string keys, then the later value for that key will overwrite the previous one." That's why. – Patrick Q Aug 15 '19 at 15:52
  • 1
    well you have to explain better what are you trying to achieve since it is unclear from you have posted. In the other side you are overriding `$data` value with the second array definition, meaning you have `$data` defined all over the places as I said be more clear on what you want to achieve so we're able to help you better – ReynierPM Aug 15 '19 at 15:52
  • 2
    To add on to the above, you should be getting undefined index notices from the second block, because after the first block there is no `$data['user_id']` or `$data['account_last_name']`. Make sure you have [error reporting](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) turned on all the way during development. – Patrick Q Aug 15 '19 at 15:58
  • `$wpdb->insert();` will only insert one row in [the WordPress Database](https://developer.wordpress.org/reference/classes/wpdb/insert). However, you can call it multiple times inside `sendinblue_runner()`. – KIKO Software Aug 15 '19 at 16:00
  • @KIKOSoftware Ahh that would be the problem then – Rob Aug 15 '19 at 16:02
  • Are you really overwriting the `$data` variable as @PatrickQ pointed out? That would be the problem. – Barmar Aug 15 '19 at 16:26

2 Answers2

0

Solved by adding an extra array:

$data_in = array(
    array(
        'id'                            => '',
        'customer_id'           => $data['user_id'],
        'event'                     => 'FIRSTNAME',
        'data'                      => $data['account_first_name'],
        'updated_in_sib'    => '0',
    ),
    array(
        'id'                            => '',
        'customer_id'           => $data['user_id'],
        'event'                     => 'LASTNAME',
        'data'                      => $data['account_last_name'],
        'updated_in_sib'    => '0',
    )
);

sendinblue_runner($data_in);

And looping through each array:

function sendinblue_runner($data_in) {
  global $wpdb;
  $table = "imp_customer_log";
  foreach( $data_in as $data ) {
    $wpdb->insert( $table, $data);
  }
}
add_shortcode('sib_runner', 'sendinblue_runner');
Rob
  • 6,304
  • 24
  • 83
  • 189
-2

you may use the array_merge function

sendinblue_runner(array_merge($first_array, $second_array));
  • 1
    How would this help? They have the same keys, so it will just replace them, not combine them in any useful way. – Barmar Aug 15 '19 at 16:25