I am looking for ways to improve the performance of this simple PHP function.
The issue is I am pulling data into an array from an API which unfortunately does not provide much scope for filtering. As a result, the dataset is initially quite large, before I loop through and unset() the key/value pairs I have no use for in my application.
Is there a better way to do this? Load time is currently 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;
Thanks in advance!
It is probably more efficient to build a new array with the properties you do want to keep. unset
is a costly operation.
Here is the pattern you could use if you want to keep a field with name booking1
(just add any other field in the same way):
$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;
An alternative way is with 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']);
You could also try with 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;
You can of course combine the two alternatives.