array_multisort 2d array
Results 1 to 7 of 7

Thread: array_multisort 2d array

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    23

    array_multisort 2d array

    Guy's

    I am trying to understand how to sort a 2 column (2d) array by the second column. My code is below but doesn't sort properly. What am I missing?



    PHP Code:
    $test_ar = array();
        
    $test_ar[0][0] = "b";
    $test_ar[0][1] = "2";
    $test_ar[1][0] = "a";
    $test_ar[1][1] = "1";
    $test_ar[2][0] = "d";
    $test_ar[2][1] = "4";
    $test_ar[3][0] = "e";
    $test_ar[3][1] = "5";        
    $test_ar[4][0] = "c";
    $test_ar[4][1] = "3";

            
    echo(
    "<pre>");
    var_dump($test_ar);
    echo(
    "</pre>");
        
        
    array_multisort($test_ar[1], SORT_ASC$test_ar[0], SORT_ASC);

    echo(
    "<pre>");
    var_dump($test_ar);
    echo(
    "</pre>"); 

  2. #2
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,853
    Well, $test_ar[0] is the array ["b", "2"] and $test_ar[1] is the array ["a", "1"]. Neither are the array you want to sort.

    Rather than Example 2, your structure is more the transpose - like that in Example 3 of the array_multisort manual page; the columns are named "volume" and "edition" instead of 0 and 1 as yours are.

    As an alternative you might want to look at Example 4 of usort (or 2) - your comparison function can of course be a lot simpler: return $a-$b;.
    Last edited by Weedpacket; 10-07-2013 at 08:43 PM.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    23
    Thanks for the information. I agree that php.net array_multisort example #3 appears to be the way my array is organized. However, the example uses an associative array and not and indexed array. Unfortunately, I tried several syntax variations of this example without success before posting my original message. The issue seems to be to find the correct syntax for the assignments in the foreach loop (red font below) for an indexed database array:

    <php>
    foreach ($data as $key => $row) {
    $volume[$key] = $row['volume'];
    $edition[$key] = $row['edition'];
    }
    </php>

    It would appear logical that if array_multisort supports associative database arrays, that it would also support indexed database arrays. Is this accurate?

  4. #4
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,853
    Quote Originally Posted by cannondale
    However, the example uses an associative array and not and indexed array
    I noted as much:
    Quote Originally Posted by Weedpacket
    the columns are named "volume" and "edition" instead of 0 and 1 as yours are.
    So you'd use 0 and 1 instead of "volume" and "edition". And of course you'd use variable names that are more appropriate for your purposes

    Incidentally, as I also alluded, using usort the code would be
    PHP Code:
    usort($test_ar, function($a$b)
    {
        return 
    $a[1] - $b[1];
    }); 
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    23
    Thanks for the additional information. I tried using the indexed notation in the foreach loop as you suggested.

    So you'd use 0 and 1 instead of "volume" and "edition".
    However, I get the following message: Warning: array_multisort(): Array sizes are inconsistent

    Code below.

    PHP Code:
    $test_ar = array();
        
    $test_ar[0][0] = "b";
    $test_ar[0][1] = "2";
    $test_ar[1][0] = "a";
    $test_ar[1][1] = "1";
    $test_ar[2][0] = "d";
    $test_ar[2][1] = "4";
    $test_ar[3][0] = "e";
    $test_ar[3][1] = "5";        
    $test_ar[4][0] = "c";
    $test_ar[4][1] = "3";

            
    foreach(
    $test_ar AS $row){
        
    $col0[$key] = $row[0];
        
    $col1[$key] = $row[1];    
    }

    array_multisort($col1SORT_ASC$col0SORT_ASC$test_ar); 

  6. #6
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,410
    Where do you define $key?

  7. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    23
    Sorry. foreach should be
    PHP Code:
    foreach($test_ar AS $key =>$row

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •