faqts : Computers : Programming : Languages : PHP : Common Problems : Arrays

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

7 of 22 people (32%) answered Yes
Recently 2 of 10 people (20%) answered Yes

Entry

How can I use 'array_multisort' to implement sql clause "order by col1 asc, col2 desc, col3 asc" ?

Jun 12th, 2001 18:54
kallioppi Garemba, Ben Udall, Juergen Ko, http://www.php.net/manual/en/function.array-multisort.php


$data = array(array("Row1 Col1", "Row2, Col1", "Row3, Col1"),
              array("Row1 Col2", "Row2, Col2", "Row3, Col2"),
              array("Row1 Col3", "Row2, Col3", "Row3, Col3"));
array_multisort($data[0], SORT_ASC, 
                $data[1], SORT_DESC,
                $data[2], SORT_ASC);
PROBLEM: This doesn't seem to work for me.
REASON : The sorted and unsorted contents of the array are identical.
MY PHP Version: 4.0.4pl1
1. SCRIPT:
------------------------------------------
<?php
$data = array(array("Row1 Col1", "Row2, Col1", "Row3, Col1"),
              array("Row1 Col2", "Row2, Col2", "Row3, Col2"),
              array("Row1 Col3", "Row2, Col3", "Row3, Col3"));
echo '<H5>$data</H5>';
print_r($data);
echo '<H5>multisort($data) </H5>';
array_multisort($data[0], SORT_ASC, 
                $data[1], SORT_DESC,
                $data[2], SORT_ASC);
print_r($data);
exit;
?>
2. OUTPUT
------------------------------------------
2.1 THIS IS THE UNSORTED ARRAY DATA
------------------------------------------
$dataArray
(
    [0] => Array
        (
            [0] => Row1 Col1
            [1] => Row2, Col1
            [2] => Row3, Col1
        )
    [1] => Array
        (
            [0] => Row1 Col2
            [1] => Row2, Col2
            [2] => Row3, Col2
        )
    [2] => Array
        (
            [0] => Row1 Col3
            [1] => Row2, Col3
            [2] => Row3, Col3
        )
)
2.2 THIS IS THE  S O R T E D  ARRAY DATA
(identical with the unsorted array data)
------------------------------------------
multisort($data) Array
(
    [0] => Array
        (
            [0] => Row1 Col1
            [1] => Row2, Col1
            [2] => Row3, Col1
        )
    [1] => Array
        (
            [0] => Row1 Col2
            [1] => Row2, Col2
            [2] => Row3, Col2
        )
    [2] => Array
        (
            [0] => Row1 Col3
            [1] => Row2, Col3
            [2] => Row3, Col3
        )
)
3. SOLUTION W/O array_multisort() FROM THE PHP MANUAL 
FOR THE usort() FUNCTION
--------------------------------------------------------
Example 2. Usort() example using multi-dimensional array 
function cmp ($a, $b) {
    return strcmp($a["fruit"],$b["fruit"]);
} 
$fruits[0]["fruit"] = "lemons";
$fruits[1]["fruit"] = "apples";
$fruits[2]["fruit"] = "grapes";
usort($fruits, "cmp"); 
while (list ($key, $value) = each ($fruits)) {
    echo "\$fruits[$key]: " . $value["fruit"] . "\n";
}
When sorting a multi-dimensional array, $a and $b contain references to 
the first index of the array. 
This example would display: 
$fruits[0]: apples
$fruits[1]: grapes
$fruits[2]: lemons