MEMO
- たつみんのMEMOφ(・ω・)
- 開発メモ
- PHP
- PHPソート
- 写真 (4)
- 犬 (3)
- 開発メモ (21)
- JavaScript (6)
- Ajax (2)
- FlashみたいなJavascript (1)
- Perl (1)
- PHP (1)
- WordPress (9)
- その他 (3)
- JavaScript (6)
6月 06 2011
PHPのソートがぐっちゃになるので、メモ。
関数名 | ソートの基準 | キーと値の相関関係 | ソート順 |
---|---|---|---|
値 | 維持しない | 昇順 | |
値 | 維持しない | 降順 | |
値 | 維持する | 昇順 | |
値 | 維持する | 降順 | |
キー | 維持する | 昇順 | |
キー | 維持する | 降順 |
■SORT_REGULAR – 通常通りに項目を比較 (型は変更しません)
■SORT_NUMERIC – 数値的に項目を比較
■SORT_STRING – 文字列として項目を比較
$myAry = $a = array('003', '1', '002','010'); $myEnum = array('333' => 'aaa', '444' => 'ccc', '222' => 'bbb', '111' => 'ddd'); echo(' <hr /> $myAry (SORT_STRING)'); var_dump($myAry); sort($myAry,SORT_STRING); var_dump($myAry); echo(' <hr /> $myEnum'); var_dump($myEnum); sort($myEnum); var_dump($myEnum);
——————————————————————————–
$myAry (SORT_STRING)
array
0 => ’003′ (length=3)
1 => ’1′ (length=1)
2 => ’002′ (length=3)
3 => ’010′ (length=3)
array
0 => ’002′ (length=3)
1 => ’003′ (length=3)
2 => ’010′ (length=3)
3 => ’1′ (length=1)
——————————————————————————–
$myEnum
array
333 => ‘aaa’ (length=3)
444 => ‘ccc’ (length=3)
222 => ‘bbb’ (length=3)
111 => ‘ddd’ (length=3)
array
0 => ‘aaa’ (length=3)
1 => ‘bbb’ (length=3)
2 => ‘ccc’ (length=3)
3 => ‘ddd’ (length=3)
$myAry = $a = array('003', '1', '002','010'); $myEnum = array('333' => 'aaa', '444' => 'ccc', '222' => 'bbb', '111' => 'ddd'); echo(' <hr /> $myAry (SORT_NUMERIC)'); var_dump($myAry); asort($myAry,SORT_NUMERIC); var_dump($myAry); echo(' <hr /> $myEnum'); var_dump($myEnum); asort($myEnum); var_dump($myEnum);
——————————————————————————–
$myAry (SORT_NUMERIC)
array
0 => ’003′ (length=3)
1 => ’1′ (length=1)
2 => ’002′ (length=3)
3 => ’010′ (length=3)
array
1 => ’1′ (length=1)
2 => ’002′ (length=3)
0 => ’003′ (length=3)
3 => ’010′ (length=3)
——————————————————————————–
$myEnum
array
333 => ‘aaa’ (length=3)
444 => ‘ccc’ (length=3)
222 => ‘bbb’ (length=3)
111 => ‘ddd’ (length=3)
array
333 => ‘aaa’ (length=3)
222 => ‘bbb’ (length=3)
444 => ‘ccc’ (length=3)
111 => ‘ddd’ (length=3)
$myAry = $a = array('003', '1', '002','010'); $myEnum = array('333' => 'aaa', '444' => 'ccc', '222' => 'bbb', '111' => 'ddd'); echo(' <hr /> $myAry'); var_dump($myAry); ksort($myAry); var_dump($myAry); echo(' <hr /> $myEnum'); var_dump($myEnum); ksort($myEnum); var_dump($myEnum);
——————————————————————————–
$myAry
array
0 => ’003′ (length=3)
1 => ’1′ (length=1)
2 => ’002′ (length=3)
3 => ’010′ (length=3)
array
0 => ’003′ (length=3)
1 => ’1′ (length=1)
2 => ’002′ (length=3)
3 => ’010′ (length=3)
——————————————————————————–
$myEnum
array
333 => ‘aaa’ (length=3)
444 => ‘ccc’ (length=3)
222 => ‘bbb’ (length=3)
111 => ‘ddd’ (length=3)
array
111 => ‘ddd’ (length=3)
222 => ‘bbb’ (length=3)
333 => ‘aaa’ (length=3)
444 => ‘ccc’ (length=3)