Based on the comments I'm not entirely sure what you ended up doing so I thought I'd provide a general example in case other folks drop by.
Sample Code and Output
<?php
function random_finish()
{
$list = array();
$list[2001] = array(200,'result for URL ID 2001');
$list[2002] = array(200,'result for URL ID 2002');
$list[2003] = array(200,'result for URL ID 2003');
$list[2004] = array(200,'result for URL ID 2004');
$list[2005] = array(200,'result for URL ID 2005');
$used = array();
while ( count($used) < count($list) )
{
$r = mt_rand(2001,2005);
if ( !isset($used[$r]) )
{
echo "... result for URL ID $r retrieved<br>\n";
$used[$r] = $list[$r];
}
}
return $used;
}
echo "<br>\n";
echo "Randomized result order...<br>\n";
$result = random_finish();
The above is meant to emulate random API response order and provides a result similar to the following:
Randomized result order...
... result for URL ID 2005 retrieved
... result for URL ID 2004 retrieved
... result for URL ID 2001 retrieved
... result for URL ID 2003 retrieved
... result for URL ID 2002 retrieved
Here, we emulate "using" the data as returned by using the key value to know which URL apply the response to. The key could be a globally unique URL ID or it could be a request sequence number:
echo "<br>\n";
echo "Use as is via request key...<br>\n";
foreach ($result as $key => $val)
echo "... URL ID $key with result code " . $val[0] . " and text '" . $val[1] . "'<br>\n";
This matches the random response order above:
Use as is via request key...
... URL ID 2005 with result code 200 and text 'result for URL ID 2005'
... URL ID 2004 with result code 200 and text 'result for URL ID 2004'
... URL ID 2001 with result code 200 and text 'result for URL ID 2001'
... URL ID 2003 with result code 200 and text 'result for URL ID 2003'
... URL ID 2002 with result code 200 and text 'result for URL ID 2002'
If a reordering was needed it can be done efficiently by using the built in function ksort:
echo "<br>\n";
echo "If needed, list after ksort...<br>\n";
ksort($result);
foreach ($result as $key => $val)
echo "... URL ID $key with result code " . $val[0] . " and text '" . $val[1] . "'<br>\n";
Which gives us an array (or list) of results sorted on the array key. This could be a request ordering value though it would be nice to have a unique ID for each URL to be processed so that the ordering would not matter.
If needed, list after ksort...
... URL ID 2001 with result code 200 and text 'result for URL ID 2001'
... URL ID 2002 with result code 200 and text 'result for URL ID 2002'
... URL ID 2003 with result code 200 and text 'result for URL ID 2003'
... URL ID 2004 with result code 200 and text 'result for URL ID 2004'
... URL ID 2005 with result code 200 and text 'result for URL ID 2005'
Some key points from a code review standpoint:
Using a unique ID for URLs can eliminate the need to do any sorting.
Sorting using a simple built-in function is likely to be more efficient than repeated lookup activities (especially for larger lists).
Choosing a key for an array instead of accepting the default numeric keys can provide a lot of value in various situations.
In a nutshell, solution design can have a huge impact on solution complexity and efficiency.