0

I just want to ask how to sort my 2D Array by column? This is my array:

$sort_arr represented by row and column: $sort_arr[$row][$column];

In my column index 3 the data is net sales and I want to output it sorted descending by net sales which is column index 3.

I will put it in here:

$size_sort = sizeof($sort_arr);
for($a=0;$a<$size_sort;$a++){
    $tabletr2 .= "<tr class='con'>
        <td align='center' > ".$rank." </td>      
        <td align='left'> ".$sort_arr [$a][$col2]." </td>      
        <td align='left'> ".$sort_arr [$a][$col2+1]." </td>      
        <td align='right'> ".$sort_arr [$a][$col2+2]." </td>
        <td align='right'> ".$sort_arr [$a][$col2+3]." </td>
        <td align='right'> ".$sort_arr [$a][$col2+4]." % </td>
        <td align='right'> ".$sort_arr [$a][$col2+5]." </td>
    </tr>";

}

Thanks Guys!

dakshbhatt21
  • 3,558
  • 3
  • 31
  • 40
Yassi
  • 2,379
  • 4
  • 24
  • 33
  • 3
    how about sorting the data before putting it into array ( e.g. sort by database query) ? – Raptor Jul 12 '13 at 09:14
  • Where does the Array come from? A Database? If so, I'd suggest using the Database query. Otherwise, maybe this of use? http://php.net/manual/en/function.array-multisort.php – Patrick Manser Jul 12 '13 at 09:14
  • no. it doesn't come from a database. – Yassi Jul 12 '13 at 09:19
  • @Yassi you to look at the `usort` with your custom function http://stackoverflow.com/questions/2699086/sort-multidimensional-array-by-value-2 – M Khalid Junaid Jul 12 '13 at 09:30

4 Answers4

0

Sort your array with usort function. You can save the column index in a session variable like this:

function cmp($a, $b)
{
    $col_index = isset($_SESSION['sort_col_index']) ? $_SESSION['sort_col_index'] : 0;
    if(isset($a[$col_index]) && isset($b[$col_index]))
    {
        // the column index is valid in both array. You can compare your data here.
        if ($a < $b)
           return -1;
        else if ($a > $b)
           return 1;
        else
           return 0;
    }
    else
        return 0;
}

// set the index column
$_SESSION['col_sort_index'] = 3;

// sort your array
usort($sort_arr, "cmp");

// display output
$size_sort = sizeof($sort_arr);
    for($a=0;$a<$size_sort;$a++){
        $tabletr2 .= "<tr class='con'>
            <td align='center' > ".$rank." </td>      
            <td align='left'> ".$sort_arr [$a][$col2]." </td>      
            <td align='left'> ".$sort_arr [$a][$col2+1]." </td>      
            <td align='right'> ".$sort_arr [$a][$col2+2]." </td>
            <td align='right'> ".$sort_arr [$a][$col2+3]." </td>
            <td align='right'> ".$sort_arr [$a][$col2+4]." % </td>
            <td align='right'> ".$sort_arr [$a][$col2+5]." </td>
            </tr>";

    }
Hieu Le
  • 8,288
  • 1
  • 34
  • 55
0

You can use array_multisort, also for 2D Arrays:

first you make a temporary Array, out of one column

and you can sort the whole array like this:

the variable $column, you set the number which column you want to sort by

foreach($sort_arr as $key => $value){
    $tempColumnArr[$key]=$v[$column];
}
array_multisort($tempColumnArr, SORT_DESC, $sort_arr );  
Nicole Stutz
  • 516
  • 4
  • 15
0

You can below trick

<?php
$sort_arr[0][0] = 'a';
$sort_arr[0][1] = 'b';
$sort_arr[0][2] = 'c';
$sort_arr[0][3] = 'd';
$sort_arr[0][4] = 'e';
$sort_arr[0][5] = 'f';


$size_sort = sizeof($sort_arr);
$col2 =0;
for($a=0;$a<$size_sort;$a++){

    $temp = $sort_arr;

    unset($temp[$a][0]);
    unset($temp[$a][1]);
    unset($temp[$a][2]);    


    $temp= array_reverse($temp[$a],true);

    $tabletr2 .= "<tr class='con'>
    <td align='center' > ".$rank." </td>      
    <td align='left'> ".$sort_arr [$a][$col2]." </td>      
    <td align='left'> ".$sort_arr [$a][$col2+1]." </td>      
    <td align='right'> ".$sort_arr [$a][$col2+2]." </td>

    <td align='right'> ".$temp[$col2]." </td>
    <td align='right'> ".$temp[$col2+1]." </td>
    <td align='right'> ".$temp[$col2+2]." </td>
    </tr>";

}
Nick
  • 84
  • 7
-1

You can use below code

$sort_arr[0][0] = 'a';
$sort_arr[0][1] = 'b';
$sort_arr[0][2] = 'c';
$sort_arr[0][3] = 'd';
$sort_arr[0][4] = 'e';
$sort_arr[0][5] = 'f';
$sort_arr[0][6] = 'g';
$sort_arr[0][7] = 'h';

$sort_arr[1][0] = 'h';
$sort_arr[1][1] = 'k';
$sort_arr[1][2] = 'z';

for($i=0; $i<count($sort_arr);$i++)
{       
    $sort_arr[$i] = array_reverse($sort_arr[$i]);

}

echo '<pre>';
print_r($sort_arr);
echo '</pre>';
Nick
  • 84
  • 7