Raphael Rafatpanah
Raphael Rafatpanah

Reputation: 19967

How to combine result sets from batch queries?

The quickbooks API only returns 1000 items for any SQL query, so I need to query in batches. My question is, how can I add the results together in php?

$customers = [];
$start_position = 1;
for ($i = 1; $i <= $number_of_queries; $i++) {
    $customers[$i] = $customer_service->query(
        $this->qb->context,
        $this->qb->realm,
        "SELECT * FROM Customer STARTPOSITION " . $start_position . " MAXRESULTS 1000"
    );
    $start_position += 1000;
}

return json_encode($customers);

Question: How can I combine $customers[1], $customers[2], $customers[3], etc. into one array containing all customer data?

Or, if it would be better to do this client side, how can this be done in JavaScript?

I've looked into array_merge and array operators but these solutions overwrite if the keys are the same, which they are.

Also, I've looked into concat in JavaScript. However, I'm not able to get this to work.

Does anyone have any experience with combining result sets from batch queries?

Screen Shot of Console

Upvotes: 1

Views: 112

Answers (1)

JME
JME

Reputation: 3642

You can try using PHP's array_merge() function.

$customers = [];
$start_position = 1;
for ($i = 1; $i <= $number_of_queries; $i++) {
    $results = $customer_service->query(
        $this->qb->context,
        $this->qb->realm,
        "SELECT * FROM Customer STARTPOSITION " . $start_position . " MAXRESULTS 1000"
    );
    $start_position += 1000;
    $customers = array_merge($customers, results);
}

return json_encode($customers);

Upvotes: 3

Related Questions