Follow Star

PHP Sorting Arrays

؟ php كيفية فرز المصفوفات من خلال وظائف الفرز في

1
sort() <=> rsort() [Uses] = sorting indexed

> order

[مفهرس] للفرز (((عناصر))) صفيف التصاعد + التنازلي .

1
asort() <=> arsort() [Uses] = sorting associative

> value

[ترابطي] للفرز (((قيم))) صفيف التصاعد + التنازلي .

1
ksort() <=> krsort() [Uses] = sorting associative

> key

[ترابطي] للفرز (((المفتاح))) صفيف التصاعد + التنازلي .

Indexed > Order

> 1. sort()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
//
$Months = array("January",
              "February",
              "March",
              "April",
               "May",
               "June",
               "July",
               "August",
               "September",
               "October",
               "November",
               "December");
               //
               sort($Months);
               print_r($Months);
               ?>
               

output : Array ( [0] => April >[1] => August >[2] => December [3] => February [4] => January [5] => July [6] => June [7] => March [8] => May [9] => November [10] => October [11] => September )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 <?php
//
  $Months = array("1000",
"980",
 "958",
  "110",
      "565",
            "990",
              "2101",
                  "3215",
                "6532",
          "565",
          "656",
        "3265");
      //
sort($Months);
          print_r($Months);
            ?>
  

Array ( [0] => 110 [1] => 565 [2] => 565 [3] => 656 [4] => 958 [5] => 980 [6] => 990 [7] => 1000 [8] => 2101 [9] => 3215 [10] => 3265 [11] => 6532 )

> 2. rsort()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
//
$Months = array("January",
              "February",
              "March",
              "April",
               "May",
               "June",
               "July",
               "August",
               "September",
               "October",
               "November",
               "December");
               //
               rsort($Months);
               print_r($Months);
               ?>
               

output : Array ( [0] => September > [1] => October > [2] => November [3] => May [4] => March [5] => June [6] => July [7] => January [8] => February [9] => December [10] => August [11] => April )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 <?php
//
  $Months = array("1000",
"980",
 "958",
  "110",
      "565",
            "990",
              "2101",
                  "3215",
                "6532",
          "565",
          "656",
        "3265");
      //
rsort($Months);
          print_r($Months);
            ?>
  

Array ( [0] => Array ( [0] => 6532 [1] => 3265 [2] => 3215 [3] => 2101 [4] => 1000 [5] => 990 [6] => 980 [7] => 958 [8] => 656 [9] => 565 [10] => 565 [11] => 110 )

Associative # Value

> asort()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
//
$Months = array("January"=>31,
              "February"=>28,
              "March"=>31,
              "April"=>30,
               "May"=>31,
               "June"=>30,
               "July"=>31,
               "August"=>31,
               "September"=>30,
               "October"=>31,
               "November"=>30,
               "December"=>31);
               //
               asort($Months);
               print_r($Months);
               ?>
               

output : Array ( [February] => 28 [April] => 30 [June] => 30 [September] => 30 [November] => 30 [January] => 31 [March] => 31 [May] => 31 [July] => 31 [August] => 31 [October] => 31 [December] => 31 )

> arsort()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
//
$Months = array("January"=>31,
              "February"=>28,
              "March"=>31,
              "April"=>30,
               "May"=>31,
               "June"=>30,
               "July"=>31,
               "August"=>31,
               "September"=>30,
               "October"=>31,
               "November"=>30,
               "December"=>31);
               //
               arsort($Months);
               print_r($Months);
               ?>
               

output : Array ( [January] => 31 [March] => 31 [May] => 31 [July] => 31 [August] => 31 [October] => 31 [December] => 31 [April] => 30 [June] => 30 [September] => 30 [November] => 30 [February] => 28 )

Associative # Key

> ksort()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
//
$Months = array("January"=>31,
              "February"=>28,
              "March"=>31,
              "April"=>30,
               "May"=>31,
               "June"=>30,
               "July"=>31,
               "August"=>31,
               "September"=>30,
               "October"=>31,
               "November"=>30,
               "December"=>31);
               //
               ksort($Months);
               print_r($Months);
               ?>
               

output : Array ( [April] => 30 [August] => 31 [December] => 31 [February] => 28 [January] => 31 [July] => 31 [June] => 30 [March] => 31 [May] => 31 [November] => 30 [October] => 31 [September] => 30 )

> krsort()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
//
$Months = array("January"=>31,
              "February"=>28,
              "March"=>31,
              "April"=>30,
               "May"=>31,
               "June"=>30,
               "July"=>31,
               "August"=>31,
               "September"=>30,
               "October"=>31,
               "November"=>30,
               "December"=>31);
               //
               krsort($Months);
               print_r($Months);
               ?>
               

output : Array ( [September] => 30 [October] => 31 [November] => 30 [May] => 31 [March] => 31 [June] => 30 [July] => 31 [January] => 31 [February] => 28 [December] => 31 [August] => 31 [April] => 30 )

Tags:

Categories:

Updated:

Comments