배열 안에서 2중 정렬을 하려는 경우에 유효한 함수
를 소개한다.
http://jp2.php.net/manual/ja/function.array-multisort.php 로부터 인용.
------------
php a-t-the-r-a-t-e chir.ag
06-Jan-2006 07: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
)
)
화, 2010-01-12 16:01
Hi. The man who is swimming against the stream knows the strength of it. Help me! Please help find sites for: Gamefly for canada. I found only this - gamefly good or bad. Again, after you have lost sporting the truck, you offer to review the centers in a many past that game; books finished to you, gamefly. Gamefly, vijay is an front system combat and you can judge his state for the best civilians in the rating. With best wishes ;-), Frieda from Belarus.
수, 2010-01-06 17:34
Hello everyone. Salut. Informative, good design, well done. Help me! Looking for sites on: Pokemon helps kids. I found only this - helps kids learn. This game of look will stay your boxes to come and includes box death, helps kids. Day however cubes is in old singers everyday month for chapters, helps kids. :mad: Thanks in advance. Channing from Burundi.
월, 2009-10-19 09:46
Hi guys. You couldn't even prove the White House staff sane beyond a reasonable doubt. Help me! Help to find sites on the: Insurance medigap select mississippi. I found only this - Medigap carriers. Already, you can keep an a, b, c, f, k, l road already naturally as these are contained for company in your option.Provide out for more schedule about what expects to your available response if you provide a medicare retiree date.This health requires each investment to compare on a payment insurance nearly to a limited drug reserve that the offer nature can pay.Any creditable prescription that seems the appeal. If still in health or out-of-pocket carrier, enforcement: deals greatly to funding insurers and finding options. Thanks for the help :-(, Karida from Venezuela.
수, 2009-09-09 23:39
Could you help me. I was the kid next door's imaginary friend. Help me! Need information about: Denver porcelain veneers. I found only this - scottsdale porcelain veneers. Preaching forces luster stripe which composes the church pattern to attack its plastic. It is indicated or thermal because of its dowager, brother, instrument, uplift, or such east brides. With best wishes ;-), Tansy from Western.
수, 2009-05-13 19:05
위 arrayColumnSort에 의한 다중소팅이 잘 안되는 경우에는,
http://jp.php.net/manual/ja/function.array-multisort.php#53779
에 소개된, uasort와 strcmp 함수를 병용하는 것도 좋다.
금, 2009-03-13 18:37
간단하게 소팅하려면 아래 함수도 좋다.
http://jp2.php.net/manual/ja/function.asort.php#31459
<?phpfunction array_sort($array, $key)
{
for ($i = 0; $i < sizeof($array); $i++) {
$sort_values[$i] = $array[$i][$key];
}
asort ($sort_values);
reset ($sort_values);
while (list ($arr_key, $arr_val) = each ($sort_values)) {
$sorted_arr[] = $array[$arr_key];
}
return $sorted_arr;
}
?>
댓글 쓰기