Sorting an array to match the order of values of another array 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')
)
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

&lt;?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) &lt; count($list) )
  {
    $r = mt_rand(2001,2005);
    if ( !isset($used[$r]) )
    {
      echo "... result for URL ID $r retrieved&lt;br&gt;\n";
      $used[$r] = $list[$r];
    }
  }

  return $used;
}

echo "&lt;br&gt;\n";
echo "Randomized result order...&lt;br&gt;\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 "&lt;br&gt;\n";
echo "Use as is via request key...&lt;br&gt;\n";
foreach ($result as $key =&gt; $val)
  echo "... URL ID $key with result code " . $val[0] . " and text '" . $val[1] . "'&lt;br&gt;\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 "&lt;br&gt;\n";
echo "If needed, list after ksort...&lt;br&gt;\n";
ksort($result);
foreach ($result as $key =&gt; $val)
  echo "... URL ID $key with result code " . $val[0] . " and text '" . $val[1] . "'&lt;br&gt;\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.