I'm running hundreds of curl operations in parallel to my own API, the response array that I get is out of order ( the reason being that the first one to finish gets added to the response array first). I need to sort these back so they correspond to the input array, in order to do that I have already written a function as shown below, I'm interested if there is a more efficient way to do this or not.
Note : The sample shows what I mean, however the input array is usually more than 100 elements long.
Sample Input :
$input = array (
'http://www.yandex.ru',
'http://www.mail.ru',
'http://www.google.com'
)
Response :
$response = array (
array('http://www.mail.ru', 200, 'some other string'),
array('http://www.yandex.ru', 200, 'some string'),
array('http://www.google.com', 200, 'yet another string')
)
Function :
function re_order($original, $scrambled) {
foreach ($scrambled as $url_response) {
$key = array_search($url_response[0],$original);
$result[$key] = $url_response;
}
ksort($result);
return $result;
}
Resulting array after calling the function (i.e. desired result).
$result = array (
array('http://www.yandex.ru', 200, 'some string'),
array('http://www.mail.ru', 200, 'some other string'),
array('http://www.google.com', 200, 'yet another string')
)