PHP 算法性能



我正在寻找提高这个简单PHP函数性能的方法。

问题是我正在将数据从 API 拉入数组中,不幸的是,该 API 没有提供太多的过滤范围。因此,在我遍历并取消设置((我在应用程序中没有用的键/值对之前,数据集最初非常大。

有没有更好的方法可以做到这一点?加载时间目前为 2000ms。

$callBokun = new callBokun($this->_op, $this->_currentDate);
$callBokun->setResponse("POST", "/booking.json/product-booking-search", "https://api.bokun.is/booking.json/product-booking-search");
$s = $callBokun->getResponse();
unset($s["tookInMillis"], $s["totalHits"], $s["totalHits"], $s["query"]);
$unset_loop = count($s["results"]);
while ($unset_loop > 0) {
    --$unset_loop;
    unset($s["results"][$unset_loop]["id"], $s["results"][$unset_loop]["externalBookingReference"], $s["results"][$unset_loop]["parentBookingId"], $s["results"][$unset_loop]["vendor"]["id"], $s["results"][$unset_loop]["vendor"]["flags"], $s["results"][$unset_loop]["productCategory"], $s["results"][$unset_loop]["productExternalId"], $s["results"][$unset_loop]["resold"], $s["results"][$unset_loop]["seller"], $s["results"][$unset_loop]["hasNotes"], $s["results"][$unset_loop]["assignedResources"], $s["results"][$unset_loop]["channel"], $s["results"][$unset_loop]["agent"], $s["results"][$unset_loop]["affiliate"], $s["results"][$unset_loop]["vessel"], $s["results"][$unset_loop]["harbour"], $s["results"][$unset_loop]["saleSource"], $s["results"][$unset_loop]["extranetUser"], $s["results"][$unset_loop]["agentUser"], $s["results"][$unset_loop]["cancellationDate"], $s["results"][$unset_loop]["cancelledBy"], $s["results"][$unset_loop]["cancelNote"], $s["results"][$unset_loop]["endDate"], $s["results"][$unset_loop]["customer"]["contactDetailsHidden"], $s["results"][$unset_loop]["customer"]["contactDetailsHiddenUntil"], $s["results"][$unset_loop]["customer"]["phoneNumberCountryCode"], $s["results"][$unset_loop]["customer"]["country"], $s["results"][$unset_loop]["customer"]["id"], $s["results"][$unset_loop]["customer"]["language"], $s["results"][$unset_loop]["customer"]["nationality"], $s["results"][$unset_loop]["customer"]["created"], $s["results"][$unset_loop]["customer"]["uuid"], $s["results"][$unset_loop]["customer"]["sex"], $s["results"][$unset_loop]["customer"]["dateOfBirth"], $s["results"][$unset_loop]["customer"]["address"], $s["results"][$unset_loop]["customer"]["postCode"], $s["results"][$unset_loop]["customer"]["state"], $s["results"][$unset_loop]["customer"]["place"], $s["results"][$unset_loop]["customer"]["organization"], $s["results"][$unset_loop]["customer"]["passportId"], $s["results"][$unset_loop]["customer"]["passportExpMonth"], $s["results"][$unset_loop]["customer"]["passportExpYear"], $s["results"][$unset_loop]["customer"]["credentials"], $s["results"][$unset_loop]["creationDate"], $s["results"][$unset_loop]["totalPrice"], $s["results"][$unset_loop]["productType"], $s["results"][$unset_loop]["customerInvoice"], $s["results"][$unset_loop]["resellerInvoice"], $s["results"][$unset_loop]["currency"], $s["results"][$unset_loop]["prepaid"], $s["results"][$unset_loop]["paidAmount"], $s["results"][$unset_loop]["resellerPaidType"], $s["results"][$unset_loop]["discountPercentage"], $s["results"][$unset_loop]["discountAmount"], $s["results"][$unset_loop]["unconfirmedPayments"], $s["results"][$unset_loop]["sellerCommission"], $s["results"][$unset_loop]["affiliateCommission"], $s["results"][$unset_loop]["agentCommission"], $s["results"][$unset_loop]["notes"], $s["results"][$unset_loop]["fields"]["pickupPlaceRoomNumber"], $s["results"][$unset_loop]["fields"]["pickupPlaceDescription"], $s["results"][$unset_loop]["fields"]["customizedPrice"], $s["results"][$unset_loop]["fields"]["dropoff"], $s["results"][$unset_loop]["fields"]["pickup"], $s["results"][$unset_loop]["fields"]["flexible"], $s["results"][$unset_loop]["fields"]["partnerBookings"], $s["results"][$unset_loop]["fields"]["startTimeId"], $s["results"][$unset_loop]["fields"]["startHour"], $s["results"][$unset_loop]["fields"]["selectedFlexDayOption"], $s["results"][$unset_loop]["fields"]["dropoffPlaceDescription"], $s["results"][$unset_loop]["fields"]["comboParent"], $s["results"][$unset_loop]["fields"]["startMinute"], $s["results"][$unset_loop]["fields"]["priceCategoryBookings"], $s["results"][$unset_loop]["boxBooking"], $s["results"][$unset_loop]["boxProduct"], $s["results"][$unset_loop]["boxSupplier"], $s["results"][$unset_loop]["contactDetailsHidden"], $s["results"][$unset_loop]["contactDetailsHiddenUntil"]);
}
$this->_bookingInfo = $s;

提前感谢!

使用要保留的属性构建一个新数组可能更有效。 unset是一项成本高昂的操作。

如果您想

保留名称为 booking1 的字段,您可以使用以下模式(只需以相同的方式添加任何其他字段(:

$s = $callBokun->getResponse();
foreach($s["results"] as $row) {
    $result[] = [
        "booking1" => $row["booking1"],
        // assign any other fields you want to keep in the same way
        // ...
    ];
}
$this->_bookingInfo = $result;

备选案文1

另一种方法是使用 array_map

$this->_bookingInfo = array_map(function ($row) {
    return [
        "booking1" => $row["booking1"],
        // assign any other fields you want to keep in the same way
        // ...
    ];
}, $s['result']);

备选案文2

您也可以尝试使用array_intersect_key

$filter = array_flip([
              'booking1', 
               // list all your desired fields here
          ]);
foreach($s["results"] as $row) {
    $result[] = array_intersect_key($row, $filter);
}
$this->_bookingInfo = $result;

您当然可以结合这两种选择。

最新更新