PHP 算法性能

PHP Algorithm Performance

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

问题是我正在将数据从 API 提取到一个数组中,不幸的是,它没有提供太多过滤范围。结果,数据集最初非常大,在我遍历并取消设置()我在我的应用程序中没有用到的 key/value 对之前。

有更好的方法吗?当前加载时间为 2000 毫秒。

$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;

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