$options = array(
array( "title" => "L", "value" => "L"),
array( "title" => "XL", "value" => "XL"),
array( "title" => "S", "value" => "S"),
array( "title" => "M", "value" => "M"),);
$options2 = array(
array( "title" => "S", "value" => "S"),
array( "title" => "M", "value" => "M"),
array( "title" => "L", "value" => "L"),
array( "title" => "XL", "value" => "XL"),);
the final data should be look like:
$options3 = array('S','M','L','XL');
I want to re-arrange the $options sort by $options2 value;
the case is look like php - sort an array by key to match another array's order by key
Both arrays have an arbitrary order. You want to re-arrange the first array to have the same order as the second array, correct ?
Alogrithm: iterate over the second array (and keep track of the current position), and for each item, you search for the equivalent item in the first array (from the current position forward) and then swap it into the current position.
Pseudo code:
If you can use extra space, then it would be more efficient using a hash map: