Google Answers Logo
View Question
 
Q: php array_multisort() function: please help me understand the syntax ( No Answer,   5 Comments )
Question  
Subject: php array_multisort() function: please help me understand the syntax
Category: Computers > Programming
Asked by: phoogoo-ga
List Price: $10.00
Posted: 18 Sep 2004 18:22 PDT
Expires: 18 Oct 2004 18:22 PDT
Question ID: 403062
I want to use array_multisort() on a multidimensional array (see
below). I want to sort the primary array by the [0] element of each
secondary array (arrayID1, 2, 3), but I don't want to sort the
secondary array at all. It seems I've been able to do this with arrays
used for creating rows and columns where the user can sort by the
column heading, but in that case I was using an associative array and
all the secondary arrays had the same number of elements. Not sure if
that's a requirement since as you can tell I have a fuzzy
understanding of how the array_multisort syntax is supposed to work.
All my efforts have been through trial and error. Hope someone can
make it crystal clear for me once and for all.

<?php

$myArray = array( array ("arrayID1", "a1", "a3", "a5", "a2", "a4", "a6"), 
			      array("arrayID3", "b1", "b3", "b4", "b2"), 
			      array("arrayID2", "c1", "c3", "c5", "c2", "c4"));

echo "<pre>";
print_r($myArray);
echo "</pre>";

array_multisort($myArray, SORT_ASC);

echo "<pre>";
print_r($myArray);
echo "</pre>";

?>

Clarification of Question by phoogoo-ga on 19 Sep 2004 17:39 PDT
This question is specifically about array_multisort(). I'm looking for
someone to explain the syntax and exactly how the function works in
detail. Thanks.

Clarification of Question by phoogoo-ga on 12 Oct 2004 20:37 PDT
I'm really looking for a clear explanation of how the
array_multisort() function works with multidimensional indexed arrays
(NOT associative arrays)where the secondary arrays are of varying
lenth.
Answer  
There is no answer at this time.

Comments  
Subject: Re: php array_multisort() function: please help me understand the syntax
From: crythias-ga on 18 Sep 2004 22:22 PDT
 
Just wanted to tell you that your program does exactly what you asked,
I think, but you might want to SORT_DESC to get arrayID1 first...

http://us2.php.net/manual/en/function.array-multisort.php

This is a free comment.
Subject: Re: php array_multisort() function: please help me understand the syntax
From: phoogoo-ga on 19 Sep 2004 08:42 PDT
 
It looks like it's working, but it's sorting them in that order
because of the varying number of elemements in each secondary array. I
changed the number of elements so you can see it better.

<?php

$myArray = array( array ("arrayID1", "a1", "a3", "a5", "a2", "a4", "a6"), 
			      array("arrayID3", "b1", "b3", "b4", "b2"), 
			      array("arrayID2", "c1", "c3", "c5", "c2", "c4", "c5", "c7", "c7"));

echo "<pre>";
print_r($myArray);
echo "</pre>";

array_multisort($myArray, SORT_ASC);

echo "<pre>";
print_r($myArray);
echo "</pre>";

?>

Here it what it prints:

Array
(
    [0] => Array
        (
            [0] => arrayID1
            [1] => a1
            [2] => a3
            [3] => a5
            [4] => a2
            [5] => a4
            [6] => a6
        )

    [1] => Array
        (
            [0] => arrayID3
            [1] => b1
            [2] => b3
            [3] => b4
            [4] => b2
        )

    [2] => Array
        (
            [0] => arrayID2
            [1] => c1
            [2] => c3
            [3] => c5
            [4] => c2
            [5] => c4
            [6] => c5
            [7] => c7
            [8] => c7
        )

)

Array
(
    [0] => Array
        (
            [0] => arrayID3
            [1] => b1
            [2] => b3
            [3] => b4
            [4] => b2
        )

    [1] => Array
        (
            [0] => arrayID1
            [1] => a1
            [2] => a3
            [3] => a5
            [4] => a2
            [5] => a4
            [6] => a6
        )

    [2] => Array
        (
            [0] => arrayID2
            [1] => c1
            [2] => c3
            [3] => c5
            [4] => c2
            [5] => c4
            [6] => c5
            [7] => c7
            [8] => c7
        )

)
Subject: Re: php array_multisort() function: please help me understand the syntax
From: crythias-ga on 19 Sep 2004 11:54 PDT
 
Using the comment on the link, I revised the code...

<?php
// $arr = array to sort.
// $col = column to sort by.
function incision_sort($arr, $col){
       for($k = 0; $k < sizeof($arr)-1; $k++){
           // $arr[$k+1] is possibly in the wrong place. Take it out.
           $t = $arr[$k+1];
           $i = $k;

           // Push $arr[i] to the right until we find the right place for $t.
           while($i >= 0 && $arr[$i][$col] > $t[$col]){
               $arr[$i+1] = $arr[$i];
               $i--;
           }

           // Insert $t into the right place.
           $arr[$i+1] = $t;
       }// End sort
       return $arr;
   }

$myArray = array( array ("arrayID1", "a1", "a3", "a5", "a2", "a4", "a6"),
                              array("arrayID3", "b1", "b3", "b4", "b2"),
                              array("arrayID2", "c1", "c3", "c5", "c2", "c4", "c
5", "c7", "c7"));
echo "<pre>";
print_r($myArray);
echo "</pre>";

//array_multisort($myArray, SORT_DESC);

//echo "<pre>";
//print_r($myArray);
//echo "</pre>";
$sortarray=incision_sort($myArray,0);
echo "sort2<pre>";
print_r($sortarray);
echo "</pre>";

?>
Result: 
Array
(
    [0] => Array
        (
            [0] => arrayID1
            [1] => a1
            [2] => a3
            [3] => a5
            [4] => a2
            [5] => a4
            [6] => a6
        )

    [1] => Array
        (
            [0] => arrayID3
            [1] => b1
            [2] => b3
            [3] => b4
            [4] => b2
        )

    [2] => Array
        (
            [0] => arrayID2
            [1] => c1
            [2] => c3
            [3] => c5
            [4] => c2
            [5] => c4
            [6] => c5
            [7] => c7
            [8] => c7
        )

)

sort2

Array
(
    [0] => Array
        (
            [0] => arrayID1
            [1] => a1
            [2] => a3
            [3] => a5
            [4] => a2
            [5] => a4
            [6] => a6
        )

    [1] => Array
        (
            [0] => arrayID2
            [1] => c1
            [2] => c3
            [3] => c5
            [4] => c2
            [5] => c4
            [6] => c5
            [7] => c7
            [8] => c7
        )

    [2] => Array
        (
            [0] => arrayID3
            [1] => b1
            [2] => b3
            [3] => b4
            [4] => b2
        )

)
Subject: Re: php array_multisort() function: please help me understand the syntax
From: phoogoo-ga on 19 Sep 2004 12:56 PDT
 
I'm familiar with this function (incision_sort) from the php.net site.
It will work for my purposes. However, my question was specifically
regarding a very clear explanation of the array_multisort() function
syntax as it relates to the array provided and my desired outcome. 
The examples and explanation on the php.net site, my reference books,
etc. don't explain it in a way that I can totally understand. I want a
php guru to explain the array_multisort() function syntax in great
detail so that my pee brain can fully grasp it.
Subject: Re: php array_multisort() function: please help me understand the syntax
From: grimm-ga on 07 Oct 2004 09:17 PDT
 
The answer you are looking for is there:

http://php-programming-goodies.euweb.cz/how-to-sort-multidimensional-array.html

Done it, tried it, it works. You can even sort by multiple field,

example:

<?

## Sorting by Rating DESC, Title ASC

$unsorted_array[] = array("Rating" => 2, "Title" => "Consulate");
$unsorted_array[] = array("Rating" => 1, "Title" => "Fury");
$unsorted_array[] = array("Rating" => 6, "Title" => "Baloon");
$unsorted_array[] = array("Rating" => 3, "Title" => "Appendix");
$unsorted_array[] = array("Rating" => 2, "Title" => "Donkey");
$unsorted_array[] = array("Rating" => 2, "Title" => "Carriage");

## Print array before sorting
print_r($unsorted_array);

## Put values to use for sorting in arrays
foreach ($unsorted_array as $array_row) {
     $sorting_values["Rating"][] = $array_row["Rating"];
     $sorting_values["Title"][] = $array_row["Title"];
}

## Sort array using sorting arrays
array_multisort($sorting_values["Rating"],SORT_DESC,$sorting_values
["Title"],SORT_ASC,$unsorted_array);

## Print array after sorting
print_r($unsorted_array);

?>

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy