2015년 4월 11일 토요일

PHP array_multisort

Case 1

<?php
$a = array("D","E","B","A","C");
array_multisort($a);
print_r($a);
Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => D
    [4] => E
)

Case 2

<?php
$a = array("D","E","B","A","C");
$b = array(1, 3, 4, 5, 2);
array_multisort($a, $b);
print_r($a);
print_r($b);
Array
(
    [0] => A
    [1] => B
    [2] => C
    [3] => D
    [4] => E
)
Array
(
    [0] => 5
    [1] => 4
    [2] => 2
    [3] => 1
    [4] => 3
)

Case 3

<?php
$a = array("D","E","B","A","C");
$b = array(1, 3, 4, 5, 2);
array_multisort($a, SORT_DESC, $b);
print_r($a);
print_r($b);
Array
(
    [0] => E
    [1] => D
    [2] => C
    [3] => B
    [4] => A
)
Array
(
    [0] => 3
    [1] => 1
    [2] => 2
    [3] => 4
    [4] => 5
)

Case 4

<?php
$a = array("B","C","A","B","A");
$b = array(1, 3, 4, 5, 2);
array_multisort($a, SORT_DESC, $b);
print_r($a);
print_r($b);
Array
(
    [0] => C
    [1] => B
    [2] => B
    [3] => A
    [4] => A
)
Array
(
    [0] => 3
    [1] => 1
    [2] => 5
    [3] => 2
    [4] => 4
)

Case 5

<?php
$a = array("B","C","A","B","A");
$b = array(1, 3, 4, 5, 2);
array_multisort($a, SORT_DESC, $b, SORT_DESC);
print_r($a);
print_r($b);
Array
(
    [0] => C
    [1] => B
    [2] => B
    [3] => A
    [4] => A
)
Array
(
    [0] => 3
    [1] => 5
    [2] => 1
    [3] => 4
    [4] => 2
)

Case 6

<?php
$a = array(1, 20, 3, 100, 56);
array_multisort($a, SORT_DESC, SORT_STRING);
print_r($a);
array_multisort($a, SORT_DESC, SORT_NUMERIC);
print_r($a);
Array
(
    [0] => 56
    [1] => 3
    [2] => 20
    [3] => 100
    [4] => 1
)
Array
(
    [0] => 100
    [1] => 56
    [2] => 20
    [3] => 3
    [4] => 1
)

Case 7

<?php
$a = array(1, "20", 3, "100", 56);
array_multisort($a, SORT_DESC, SORT_STRING);
var_dump($a);
array_multisort($a, SORT_DESC, SORT_NUMERIC);
var_dump($a);
array(5) {
  [0]=>
  int(56)
  [1]=>
  int(3)
  [2]=>
  string(2) "20"
  [3]=>
  string(3) "100"
  [4]=>
  int(1)
}
array(5) {
  [0]=>
  string(3) "100"
  [1]=>
  int(56)
  [2]=>
  string(2) "20"
  [3]=>
  int(3)
  [4]=>
  int(1)
}

Case 8

<?php
$a = array(1, "20", 3, "100", 56);
$b = array("A", "2", 1, "B", 3);
array_multisort($a, SORT_DESC, SORT_STRING, $b);
var_dump($a);
var_dump($b);
array_multisort($a, SORT_DESC, SORT_NUMERIC, $b);
var_dump($a);
var_dump($b);
array(5) {
  [0]=>
  int(56)
  [1]=>
  int(3)
  [2]=>
  string(2) "20"
  [3]=>
  string(3) "100"
  [4]=>
  int(1)
}
array(5) {
  [0]=>
  int(3)
  [1]=>
  int(1)
  [2]=>
  string(1) "2"
  [3]=>
  string(1) "B"
  [4]=>
  string(1) "A"
}
array(5) {
  [0]=>
  string(3) "100"
  [1]=>
  int(56)
  [2]=>
  string(2) "20"
  [3]=>
  int(3)
  [4]=>
  int(1)
}
array(5) {
  [0]=>
  string(1) "B"
  [1]=>
  int(3)
  [2]=>
  string(1) "2"
  [3]=>
  int(1)
  [4]=>
  string(1) "A"
}

Read More

댓글 없음:

댓글 쓰기