I was able to work this out quite simply.
I put the array_merge()
into a $results
variable:
$results = array_merge($view->result, $first_extra_results_view->result);
To get the $order_number
for each of the $results
, I used array_column():
$order_number = array_column($results, 'commerce_order_order_number');
Then to sort the $results
by $order_number
I used array_multisort().:
array_multisort($order_number, SORT_DESC, $results);
I did also have to re-sort the entire array for some reason, but all in all this is what I came up with:
function hook_views_pre_render(ViewExecutable $view) {
if ($view->id() == 'commerce_orders' && $view->current_display == 'data_export_1') {
$first_extra_results_view = Views::getView('commerce_orders');
$first_extra_results_view->setDisplay('data_export_2');
$first_extra_results_view->execute();
$first_extra_results_view->result;
$results = array_merge($view->result, $first_extra_results_view->result);
// Sort the rows by order number.
$order_number = array_column($results, 'commerce_order_order_number');
array_multisort($order_number, SORT_DESC, $results);
// Sort the rows so that shipping items are back on the bottom.
krsort($results);
$view->result = $results;
}
}