Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How could I sort a defined array based on another array? In our CMS, we have a form that uses the jQuery sortable widget to sort the rows in the form. The form rows are initially created with a multidimensional array. Upon saving, the data is saved in a serialized array. So whenever you sort the rows and save, the data is saved in that "sorted" order, which is how it should be. The problem we're hitting is the form is still output in the order from the original array, not the saved, sorted array.

Would there be a way to sort the original array in the order of the saved, sorted array?

Here's how the original array and order looks:

Array
(
    [group-1] => Array
        (
            [fields] => Array
                (
                    [name] => Array
                        (
                            [type] => text
                            [name] => Full Name
                        )

                    [email-address] => Array
                        (
                            [type] => text
                            [name] => Email Address
                        )

                )
        )

    [group-2] => Array
        (
            [fields] => Array
                (
                    [address] => Array
                        (
                            [type] => text
                            [name] => Address
                        )

                )
        )

    [group-3] => Array
        (
            [fields] => Array
                (
                    [city] => Array
                        (
                            [type] => text
                            [name] => City
                        )

                    [zip] => Array
                        (
                            [type] => text
                            [name] => Zip
                        )

                )
        )

)

And here is the sorted saved data

Array
(
    [group-3] => Array
        (
            [city] => Apples
            [zip] => 12345
        )

    [group-1] => Array
        (
            [name] => Tigre Woodrow
            [email-address] => [email protected]
        )

    [group-2] => Array
        (
            [address] => 1234 Anywhere Street
        )

)

So from the arrays, the rows of fields are the group id's there. Would there be a way to sort based on that group id?

share|improve this question
1  
Use usort. –  mc10 Jan 31 at 1:25
    
I've tried playing with usort, but really don't know how it works with two multi-dimensional arrays. –  Ryan Palmer Jan 31 at 2:07
add comment

1 Answer

up vote 1 down vote accepted
+50

The below comment in the usort documentation sounds like it might help you out. I'll try to update the code soon specific to your problem but it might push you in the right direction.

If you want to sort an array according to another array acting as a priority list, you can use this function.

<?php 
function listcmp($a, $b) 
{ 
  global $order; 

  foreach($order as $key => $value) 
    { 
      if($a==$value) 
        { 
          return 0; 
          break; 
        } 

      if($b==$value) 
        { 
          return 1; 
          break; 
        } 
    } 
} 

$order[0] = "first"; 
$order[1] = "second"; 
$order[2] = "third"; 

$array[0] = "second"; 
$array[1] = "first"; 
$array[2] = "third"; 
$array[3] = "fourth"; 
$array[4] = "second"; 
$array[5] = "first"; 
$array[6] = "second"; 

usort($array, "listcmp"); 

print_r($array); 
?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.