PHP usort()

1 개요[ | ]

PHP usort()
  • Sort an array by values using a user-defined comparison function
function cmp($a, $b) {
    if ($a == $b) return 0;
    return ($a < $b) ? -1 : 1;
}

$a = [3, 2, 5, 6, 1];
usort($a, "cmp");

foreach ($a as $key => $value) {
    echo "$key: $value\n";
}
function cmp($a, $b) {
    return strcmp($a["fruit"], $b["fruit"]);
}

$fruits[0]["fruit"] = "lemons";
$fruits[1]["fruit"] = "apples";
$fruits[2]["fruit"] = "grapes";
usort($fruits, "cmp");

print_r($fruits);

2 같이 보기[ | ]

3 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}