-3
$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

15
  • Have you tried something and what would be the expected output? Commented Jun 9, 2015 at 9:52
  • @Rizier123 the result should be same as $options2 Commented Jun 9, 2015 at 9:53
  • If you already have $options2 why can't you just use this array? Commented Jun 9, 2015 at 9:54
  • @Rizier123 $options is generated by system, and I cannot edit output from the system.. bu I have standard data by $options2, so I need rearrange $options look like $options2; Commented Jun 9, 2015 at 9:56
  • is it generated from database? Commented Jun 9, 2015 at 9:58

2 Answers 2

0

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:

for (curr_pos=0; curr_pos<options2.length; curr_pos++)
  for (pos=curr_pos; pos<options.length; pos++)
    if options2[curr_pos]==options[pos]:
      swap options[curr_pos], options[pos]
      break

If you can use extra space, then it would be more efficient using a hash map:

h=new HashMap()
for (pos=0; pos<options.length; pos++)
  h[options[pos].key]=options[pos].val
for (pos=0; pos<options2.length; pos++)
  options3[pos]= make_pair(options2[pos].key, h[options2[pos].key])
Sign up to request clarification or add additional context in comments.

2 Comments

p.s. this can also be done quicker by putting the items of the first array into a hash table, and using that to find their position. This would make the algorithm O(N) time and O(N) space, instead of O(N^2) time, O(1) space as shown above.
yes I want to arrange the first array same order as the second
0

This can be done using array_shift php function.

Please use custom function rearrange_array.

function rearrange_array($array, $key) {
		while ($key > 0) {
			$temp = array_shift($array);
			$array[] = $temp;
			$key--;
		}
		return $array;
	}
	
	
	$finalArray = rearrange_array($options,2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.