Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

PHP | asort,arsort

概要

asort,arsort

詳細

asort,arsort関数で配列をソートする

サンプル

<?php
$numbers = array(2,3,1);
print_r($numbers);
print("\n");
asort($numbers);
print_r($numbers);
print("\n");
arsort($numbers);
print_r($numbers);
print("\n");

出力

Array
(
    [0] => 2
    [1] => 3
    [2] => 1
)

Array
(
    [2] => 1
    [0] => 2
    [1] => 3
)

Array
(
    [1] => 3
    [0] => 2
    [2] => 1
)

Rubyと比較

# encoding: utf-8
numbers = [2, 3, 1];
p numbers
p numbers.sort
p numbers.sort.reverse