J
J
Jesse Pinkman2021-06-21 19:26:35
PHP
Jesse Pinkman, 2021-06-21 19:26:35

How to prepare query string for multiple insert?

I have an array, for example:

[0] => Array
        (
            [stock_id] => 846
            [order_count] => 2
            [order_price] => 22
            [total] => 44
            [profit] => 2
            [order_date] => 21.06.2021
            [short_date] => 06.2021
        )

    [1] => Array
        (
            [stock_id] => 841
            [order_count] => 1
            [order_price] => 54
            [total] => 54
            [profit] => 53
            [order_date] => 21.06.2021
            [short_date] => 06.2021
        )


For pdo, I need to get a request like:

VALUES (?, ?, ?, ?, ?, ?, ?),
              (?, ?, ?, ?, ?, ?, ?),
              .. и тд, столько, сколько записей в массиве


Sandbox: - My attempts

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lander, 2021-06-21
@jessepinkman010101

$values = implode(',', array_map(
  function ($line) {
    return '(' . implode(',', array_fill(0, count($line), '?')) . ')';
  },
  $arr_data
));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question