0

I have the following array:

Array
(
[Bridge Work] => Array
    (
        [0] => Array
            (
                [Name] => NJ Trunpike_Bridge Repair Work
                [Location] => New Jersey
                [State] => New Jersey
            )

        [1] => Array
            (
                [Name] => Honoapiilani Highway Bridge Truss
                [Location] => Maui
                [State] => Hawai
            )

        [2] => Array
            (
                [Name] => BlueCross Blueshield of Tennessee (Bridge)
                [Location] => Memphis
                [State] => Tennessee
            )

        [3] => Array
            (
                [Name] => Henderson Center Connector Bridge
                [Location] => Coquitlam
                [State] => British Columbia
            )

    )

[Educational] => Array
    (
        [0] => Array
            (
                [Name] => RTI TASS Complex Admin Bldg
                [Location] => Bluffdale
                [State] => Utah
            )

        [1] => Array
            (
                [Name] => Auburn High School
                [Location] => Auburn
                [State] => Washington
            )

        [2] => Array
            (
                [Name] => Reed College
                [Location] => Portland
                [State] => Oregon
            )

        [3] => Array
            (
                [Name] => Shorewood High School
                [Location] => Shoreline
                [State] => Washington
            )

    )

)

Taking in consideration key State and its value, I want to sort it in ascending order.

Expected output:

Array
(
[Bridge Work] => Array
    (
        [0] => Array
            (
                [Name] => Henderson Center Connector Bridge
                [Location] => Coquitlam
                [State] => British Columbia
            )

        [1] => Array
            (
                [Name] => Honoapiilani Highway Bridge Truss
                [Location] => Maui
                [State] => Hawai
            )

        [2] => Array
            (
                [Name] => NJ Trunpike_Bridge Repair Work
                [Location] => New Jersey
                [State] => New Jersey
            )

        [3] => Array
            (
                [Name] => BlueCross Blueshield of Tennessee (Bridge)
                [Location] => Memphis
                [State] => Tennessee
            )

    )

[Educational] => Array
    (
        [0] => Array
            (
                [Name] => Reed College
                [Location] => Portland
                [State] => Oregon
            )

        [1] => Array
            (
                [Name] => RTI TASS Complex Admin Bldg
                [Location] => Bluffdale
                [State] => Utah
            )

        [2] => Array
            (
                [Name] => Auburn High School
                [Location] => Auburn
                [State] => Washington
            )

        [3] => Array
            (
                [Name] => Shorewood High School
                [Location] => Shoreline
                [State] => Washington
            )

    )

)

My attempts:

Using usort():

function cmp($a, $b) 
{
    return $a["State"] - $b["State"];
}

usort($project_archives, "cmp");

echo '<pre>'; print_r($project_archives);

And using a loop combined with asort():

function aasort(&$array, $key)
{
    $sorter = array();
    $ret    = array();

    reset($array);

    foreach ($array as $ii => $va) {
        $sorter[$ii] = $va[$key];
    }

    asort($sorter);

    foreach ($sorter as $ii => $va) {
        $ret[$ii] = $array[$ii];
    }

    $array = $ret;
    return $array;
}

$sort = aasort($project_archives, "State");
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
Parag Tyagi
  • 8,780
  • 3
  • 42
  • 47
  • What do you try? http://stackoverflow.com/questions/19306741/sorting-multidimensional-array-based-on-the-order-of-plain-array?rq=1 – Christian May 28 '14 at 15:22
  • 1
    Can you explain to us what you've tried so far? The best Stack Overflow questions show that you've done your research, and this information also ensures that you get the best possible answers as quickly as possible and that you don't get answers that you have already tried. It also gives us a place to start in helping you. – Amal Murali May 28 '14 at 15:23
  • Use usort() against each subarray('Bridge Work', 'Educational') individually – Mark Baker May 28 '14 at 15:26
  • @MarkBaker: I tried somewhat like that. Please check edits, what have I done wrong there? – Parag Tyagi May 28 '14 at 15:33
  • 1
    Start by remembering that you're comparing strings, not numbers..... so use a function like [strcmp()](http://www.php.net/manual/en/function.strcmp.php) rather than using subtraction.... `function cmp($a, $b) { return strcmp($a["State"],$b["State"]); } usort($project_archives['Educational'], "cmp"); usort($project_archives['Bridge Work'], "cmp");` – Mark Baker May 28 '14 at 15:36
  • @MarkBaker, AmalMurali - I've added the whole array [here](http://3v4l.org/HfTZj). Please instruct. – Parag Tyagi May 28 '14 at 15:39

1 Answers1

3

Alternatively, you could use a array_multisort() to sort those values inside the array (the child arrays) in ascending. Consider this example:

$original_values = array( 'Bridge Work' => array( array('Name' => 'NJ Trunpike_Bridge Repair Work', 'Location' => 'New Jersey', 'State' => 'New Jersey'), array('Name' => 'Honoapiilani Highway Bridge Truss', 'Location' => 'Maui', 'State' => 'Hawaii'), array('Name' => 'BlueCross Blueshield of Tennessee (Bridge)', 'Location' => 'Memphis', 'State' => 'Tennessee'), array('Name' => 'Henderson Center Connector Bridge', 'Location' => 'Coquitlam', 'State' => 'British Columbia'), ), 'Educational' => array( array('Name' => 'RTI TASS Complex Admin Bldg', 'Location' => 'Bluffdale', 'State' => 'Utah'), array('Name' => 'Auburn High School', 'Location' => 'Auburn', 'State' => 'Washington'), array('Name' => 'Reed College', 'Location' => 'Portland', 'State' => 'Oregon'), array('Name' => 'Shorewood High School', 'Location' => 'Shoreline', 'State' => 'Washington'), ),);

$sorted_values = array();
foreach($original_values as $key => $value) {
    $columns = null;
    foreach ($value as $index => $element) {
        $columns[] = $element['State'];
    }
    $temp = $value;
    array_multisort($columns, SORT_ASC, $temp);
    $sorted_values[$key] = $temp;
}

echo '<pre>';
print_r($sorted_values);
echo '</pre>';

Sample Code

user1978142
  • 7,946
  • 3
  • 17
  • 20