I need optimization of loop (array) in loop (object). Below is my solution which is working, but if I try this with huge amount of data then is too slow.
Here is my array
$data = [
["PLZ", "Preis"],
["8074", "90"],
["8075", "90"],
["8076", "90"],
["8077", "90"],
["8078", "77"],
["1010", "77"],
["1020", "77"],
["1030", "77"],
["8041", "55"],
["8020", "89"],
];
Here is my object
$postal_collection = {
"1010":1,
"1020":2,
"1030":3,
"8020":1602,
"8041":1604,
"8074":1622,
"8075":1623,
"8076":1624,
"8077":1625
}
Here is working loop
$allData = [];
foreach ($data as $key => $fields) {
foreach ($postal_collection as $postal => $placeId) {
if ($fields[0] == $postal) {
$allData[$placeId] = [
'postal' => $postal,
'place_id' => $placeId,
'price' => $fields[1],
];
}
}
}
So how can I change this loop to make the same job but faster?
array_mapmaybe using a callback that return you the expecting result ?$postal_collection[$fields[0]].