라라벨 array_sort()

1 개요[ | ]

라라벨 array_sort()
# 라라벨 5.5+에서 가능
$array = ['Desk', 'Table', 'Chair'];
$sorted = array_sort($array);
# ['Chair', 'Desk', 'Table']
$array = [
    ['id'=>1, 'name'=>'Carol'],
    ['id'=>2, 'name'=>'Alice'],
    ['id'=>3, 'name'=>'Bob'],
];
$sorted = array_values(array_sort($array, function ($value) {
    return $value['name'];
}));
dd( $sorted );
# array:3 [
#   0 => array:2 [
#     "id" => 2
#     "name" => "Alice"
#   ]
#   1 => array:2 [
#     "id" => 3
#     "name" => "Bob"
#   ]
#   2 => array:2 [
#     "id" => 1
#     "name" => "Carol"
#   ]
# ]
$array = [
    ['id'=>1, 'name'=>'Carol'],
    ['id'=>2, 'name'=>'Alice'],
    ['id'=>3, 'name'=>'Bob'],
];
$sorted = collect($array)->sortBy('name')->values()->toArray();
dd( $sorted );
# array:3 [
#   0 => array:2 [
#     "id" => 2
#     "name" => "Alice"
#   ]
#   1 => array:2 [
#     "id" => 3
#     "name" => "Bob"
#   ]
#   2 => array:2 [
#     "id" => 1
#     "name" => "Carol"
#   ]
# ]

2 같이 보기[ | ]

3 참고[ | ]

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