Solved

Sorting a multi-dimensional array

Posted on 2013-01-16
4
295 Views
Last Modified: 2013-01-16
Hello,

I have the following array:

Array
(
    [0] => Array
        (
            [0] => 3
            [1] => City0
            [2] => 64
        )

    [1] => Array
        (
            [0] => 6
            [1] => City1
            [2] => 164
        )

    [2] => Array
        (
            [0] => 7
            [1] => City2
            [2] => 160
        )

    [3] => Array
        (
            [0] => 8
            [1] => City3
            [2] => 188
        )

    [4] => Array
        (
            [0] => 9
            [1] => City4
            [2] => 156
        )

    [5] => Array
        (
            [0] => 10
            [1] => City5
            [2] => 72
        )

)

Open in new window


All I need to do is to sort the arrays with the [2] value so that the cities are sorted as:

City3
City1
City2
City4
City5
City0

Is that possible?

Thanks for the help!
0
Comment
Question by:infodigger
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
Slimshaneey earned 500 total points
ID: 38783252
I use this function for exactly that reason:

private function aasort(&$array, $key) {
        if (is_array($array)) {
            $sorter = array();
            $ret = array();
            reset($array);
            foreach ($array as $ii => $va) {
                $sorter[$ii] = $va[$key];
            }
            arsort($sorter);
            foreach ($sorter as $ii => $va) {
                $ret[$ii] = $array[$ii];
            }
            //Reset array keys to zero base...
            $array = array_values($ret);
            return $array;
        }
    }

Open in new window


In your case the 2 parameters are the array, and 2 as thats the key you want to sort it on
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 38783279
<?php

$src = array(
	array(3,"City0",64),
	array(6,"City1",160),
	array(7,"City2",160),
	array(8,"City3",188),
	array(9,"City4",156),
	array(10,"City5",72),
);

$sorted_array = mdarray_sort($src,2,SORT_ASC);
print_r($sorted_array);

function mdarray_sort($srcArray,$sortColumn,$sortDirection = 4)
{
     $arrSortData = array();
     foreach($srcArray as $srcIDX => $srcRow)
          $arrSortData[$srcIDX] = $srcRow[$sortColumn];
     array_multisort($arrSortData,$sortDirection,$srcArray);
     return $srcArray;
}
?>

Open in new window

0
 

Author Closing Comment

by:infodigger
ID: 38783287
Thank you very much for the answer!
0
 
LVL 34

Expert Comment

by:gr8gonzo
ID: 38783290
That example uses array_multisort(), which is the built-in PHP function for doing the bulk of this work and doing it quickly and efficiently. My mdarray_sort function is just a wrapper around it. I've also used this function in the past, which supports multiple sorts (I did not write it, though):

function array_csort() {        //coded by Ichier2003
    $args = func_get_args();
    $marray = array_shift($args); 
    $msortline = 'return(array_multisort(';
    foreach ($args as $arg) {
        $i++;
        if (is_string($arg)) {
            foreach ($marray as $row) {
                $sortarr[$i][] = $row[$arg];
            }
        } else {
            $sortarr[$i] = $arg;
        }
        $msortline .= '$sortarr['.$i.'],';
    }
    $msortline .= '$marray));';
    eval($msortline);
    return $marray;
}

Open in new window

0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Responsive Design Tools 6 36
MySQL database data submission 7 39
Codiing Non-Existent Links 4 23
showing numeric numbers 2 8
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now