Php仅在结果满足条件时执行代码

I have a shop checkout function something like this:

    $min_delivery = 25;
    foreach ($shoppingCartItem as $item) {
     $insert->name = $item->name;
     $insert->price = $item->price;
     $insert->quantity = $item->qty;
     $insert->save();
     $total += $item->price*$item->qty;
    }

Is there any php function that would allow the foreach loop to happen only if ($total > $min_delivery) .

Or the only way would be to do the foreach twice, once only to calculate $total, then if ($total > $min_delivery) do a second foreach to insert into the database.

*EDIT - some details on why I want some other way instead of two loops:

The issue is that I can't trust the $item->price from the shopping cart because it comes from the user (and I don't verify it until checkout) so I need to check it against database before inserting.

So doing the loop twice would mean to query the database twice.

Here's a possible solution

$total = array_reduce($shoppingCartItem,
    function($carry,$item) {
        return $carry + $item->price*$item->qty;
    }
);
$min_delivery = 25;
if ($total > $min_delivery) {
    ...
}