Portrety Uliczne Nieznajomych - zobacz wyjątkową galerię portretów z warszawskich ulic
ZALOGUJ SIĘ
login:
hasło:
przypomnij hasło
załóż konto użytkownika
(i zobacz kilka porad gratis)
   
WYSZUKIWARKA I DZIAŁY
całe porady  tytuły
zaznacz działy do przeszukania
(brak wyboru = wszystkie działy)
PHP
MySQL >
PostgreSQL
SQLite
Perl
Java
XML
XSLT
XPath
WML
SVG
RegExp
Wyszukiwarki
Ochrona
VBScript
Google Plus
XHTML/CSS
JavaScript
Grafika
Flash
Photoshop
Windows
Linux
Bash
Apache
Procmail
E-biznes
Explorer
Opera
Firefox
Inne porady
   
KURSY, DOKUMENTACJE
Własne:
XHTML/CSS
JavaScript
ActionScript
WML, RSS, SSI
Pozostałe:
PHP
MySQL
Java API
więcej...
   
użytkowników online: 65
W CZYM MOGĘ POMÓC?


   
OPINIE UŻYTKOWNIKÓW
Porady zamieszczone tutaj przez Darka są pomocne w wielu chwilach. Wielokrotnie tworząc jakiś złożony serwis korzystam z tych porad. Można by tworzyć samemu te skrypty, ale tak naprawdę czy nie lepiej jest wziąć skrypt z tej strony i zmodyfikowac go dla swoich potrzeb? Wprawdzie możemy taki skrypt napisać sami, ale po co, skoro stracimy czas na coś, co ktoś juz napisał, przetestował i może zagwarantować, że działa poprawnie. Któryś raz z rzędu opłacam abonament i nie raz jeszcze opłacę. Kawał dobrej roboty i ogrom wiedzy w jednym miejscu.

Piotr Karamański
Design Studio

   
GALERIA FOTOGRAFII
   
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net

[Spis] [A] [B] [C] [D] [E] [F] [G] [H] [I] [J] [K] [L] [M] [N] [O] [P] [Q] [R] [S] [T] [U] [V] [X] [W] [Z]

array_multisort

(PHP 4, PHP 5)

array_multisort -- Sortuje wiele tablic lub wielowymiarowe tablice

Opis

bool array_multisort ( array tbl1 [, mixed arg [, mixed ... [, array ...]]] )

array_multisort() może być użyta do sortowania kilku tablic na raz lub wielowymiarowej tablicy na podstawie jednego z większej liczby wymiarów. Zachowywane są przypisania kluczy.

Tablice wejściowe są traktowane jak kolumy tablicy, które mają być posortowane wierszami - odpowiada to funkcjonalności warunku SQL ORDER BY. Pierwsza tablica jest tablicą priorytetową do sortowania. Wiersze (wartości) w tej tablicą które są takie sane sortowane są według następnej tablicy wejściowej i tak dalej.

Struktura argumentów tej funkcji nie jest zwyczajna, ale jest ona elastyczna. Pierwszy argument musi być tablicą. Każdy następny argument musi być tablicą lub flagą oznaczającą porządek sortowania - jeden z poniższych.

Flagi porządku sortowania:

  • SORT_ASC - sortuj w porządku rosnącym

  • SORT_DESC - sortuj w porządku malejącym

Flagi typu sortowania:

  • SORT_REGULAR - porównuj elementy normalnie

  • SORT_NUMERIC - porównuj elementy numerycznie

  • SORT_STRING - porówuj elementy jak stringi

Nie można podać żadnych dwóch flag tego samego typu dla jednej tablicy. Flagi sortowania podane pod argumencie-tablicy dotyczą tylko tej tablicy - są one zerowane do domyślnych wartośći SORT_ASC i SORT_REGULAR po każdym argumencie tablicowym.

Zwraca TRUE w przypadku sukcesu, FALSE w przypadku porażki.

Przykład 1. Sortowanie wielu tablic

$tbl1 = array ("10", 100, 100, "a");
$tbl2 = array (1, 3, "2", 1);
array_multisort ($tbl1, $tbl2);

W tym przypadku, po sortowaniu, pierwsza tablica będzie zawierać 10, "a", 100, 100, a druga 1, 1, "2", 3. Elementy drugiej tablicy odpowiadające identycznym elementom pierwszej tablicy (100 i 100) także zostały posortowane.

Przykład 2. Sortowanie wielowymiarowych tablic

$tbl = array (array ("10", 100, 100, "a"), array (1, 3, "2", 1));
array_multisort ($tbl[0], SORT_ASC, SORT_STRING,
                 $tbl[1], SORT_NUMERIC, SORT_DESC);

W tym przykładzie, po sortowaniu, pierwsza tablica zawiera 10, 100, 100, "a" (została posortowana według wartości tekstowych w porządku rosnącym), a druga 1, 3, "2", 1 (sortowana jak liczby w porządku malejącym).




User Contributed Notes

mail at theopensource dot com
31-Jan-2006 08:34

I wanted to share with you a function that I created to make the array_multisort process much easier for myself... There was some interesting things that I encountered and I will post that in the comments.

I created this function so that all I have to do is tell it what column I want to sort through in a one level deep multidimensional array.  You can Try this code in your browser to view the results

ex/
<?php

//Here is an array example
$test[0]['name'] = "David";
$test[0]['age'] = 28;
$test[1]['name'] = "Dennis";
$test[1]['age'] = 23;
$test[2]['name'] = "Joseph";
$test[2]['age'] = 42;

//Here is the Function

function sortmddata($array, $by, $order, $type){

//$array: the array you want to sort
//$by: the associative array name that is one level deep
////example: name
//$order: ASC or DESC
//$type: num or str
      
$sortby = "sort$by"; //This sets up what you are sorting by

$firstval = current($array); //Pulls over the first array

$vals = array_keys($firstval); //Grabs the associate Arrays

foreach ($vals as $init){
  
$keyname = "sort$init";
   $
$keyname = array();
}
//This was strange because I had problems adding
//Multiple arrays into a variable variable
//I got it to work by initializing the variable variables as arrays
//Before I went any further

foreach ($array as $key => $row) {
  
foreach (
$vals as $names){
  
$keyname = "sort$names";
  
$test = array();
  
$test[$key] = $row[$names];
   $
$keyname = array_merge($$keyname,$test);
  
}

}

//This will create dynamic mini arrays so that I can perform
//the array multisort with no problem
//Notice the temp array... I had to do that because I
//cannot assign additional array elements to a
//varaiable variable           

if ($order == "DESC"){   
if (
$type == "num"){
array_multisort($$sortby,SORT_DESC, SORT_NUMERIC,$array);
} else {
array_multisort($$sortby,SORT_DESC, SORT_STRING,$array);
}
} else {
if (
$type == "num"){
array_multisort($$sortby,SORT_ASC, SORT_NUMERIC,$array);
} else {
array_multisort($$sortby,SORT_ASC, SORT_STRING,$array);
}
}

//This just goed through and asks the additional arguments
//What they are doing and are doing variations of
//the multisort

return $array;
}

//Now to test it

$test = sortmddata($test,'age','ASC','num');

print_r ($test);

//This will return
//Array (
//[0] => Array ([name] => Dennis [age] => 23 )
//[1] => Array ( [name] => David [age] => 28 )
//[2] => Array ( [name] => Joseph [age] => 42 )
//)

?>

There you go... please let me know what you think if you like.


php a-t-the-r-a-t-e chir.ag
05-Jan-2006 11:10

Re: phu at kungphu, 19-Dec-2005 11:36

asort($test) will not let me specify which columns to sort ASC/DESC, NUMERIC/STRING etc.

I have data similar to what you specified. Now I want to sort $test by points DESC and name ASC. Here's my function that does it, based on suggestions on this page. It uses array_multisort (and hence acts just like it: preserving string-keys etc.)

<?php

 
function arrayColumnSort()
  {
  
$n = func_num_args();
  
$ar = func_get_arg($n-1);
   if(!
is_array($ar))
     return
false;

   for(
$i = 0; $i < $n-1; $i++)
    
$col[$i] = func_get_arg($i);

   foreach(
$ar as $key => $val)
     foreach(
$col as $kkey => $vval)
       if(
is_string($vval))
         ${
"subar$kkey"}[$key] = $val[$vval];

  
$arv = array();
   foreach(
$col as $key => $val)
    
$arv[] = (is_string($val) ? ${"subar$key"} : $val);
  
$arv[] = $ar;

  
call_user_func_array("array_multisort", $arv);
   return
$ar;
  }

 
$test["pete"]['points']=1;
 
$test["pete"]['name']='Peter';

 
$test["mike"]['points']=5;
 
$test["mike"]['name']='Mike';

 
$test["zoo"]['points']=2;
 
$test["zoo"]['name']='John Zoo';

 
$test["ab"]['points']=2;
 
$test["ab"]['name']='John Ab';

 
$test1 = $test;

 
asort($test1);

 
$test2 = arrayColumnSort("points", SORT_DESC, SORT_NUMERIC, "name", SORT_ASC, SORT_STRING, $test);

 
print_r($test1); // asort
 
print_r($test2); // arrayColumnSort

?>

Output from asort:

Array
(
   [pete] => Array
       (
           [points] => 1
           [name] => Peter
       )

   [ab] => Array
       (
           [points] => 2
           [name] => John Ab
       )

   [zoo] => Array
       (
           [points] => 2
           [name] => John Zoo
       )

   [mike] => Array
       (
           [points] => 5
           [name] => Mike
       )

)

Output from arrayColumnSort:

Array
(
   [mike] => Array
       (
           [points] => 5
           [name] => Mike
       )

   [ab] => Array
       (
           [points] => 2
           [name] => John Ab
       )

   [zoo] => Array
       (
           [points] => 2
           [name] => John Zoo
       )

   [pete] => Array
       (
           [points] => 1
           [name] => Peter
       )

)


phu at kungphu
19-Dec-2005 08:36

Notepad's example using asort/arsort will -not- work unless 'points' is defined -before- 'name'.

Running the posted code sorts by 'name', which was not the point of the previous posts.  Defining 'points' as 'apoints' also did not work; however, defining 'points' first yields correct sorting:

$test[0]['points']=1;
$test[0]['name']='Peter';

$test[1]['points']=5;
$test[1]['name']='Mike';

$test[2]['points']=2;
$test[2]['name']='John';

asort($test);

It appears asort uses the first defined element to sort a multidimensional array.


notepad at codewalkers dot com
10-Dec-2005 01:33

for you guys trying to sort scores on an associative multi-dimensional array, why are you creating your own functions?

<?php

$test
[0]['name']='Peter';
$test[0]['points']=1;

$test[1]['name']='Mike';
$test[1]['points']=5;

$test[2]['name']='John';
$test[2]['points']=2;

asort($test);
// or even arsort();

?>

the above seems to work for me...


Shmee
30-Sep-2005 03:46

RWC

 

 
  © 1996-2012 & Reporter.plmiejscao serwisieabonamentwarunki korzystaniaRSSkontakt