PHP - Ds Map::sorted() 函式



PHP 的 Ds\Map::sorted() 函式用於檢索按值排序的地圖副本。返回的地圖的值預設情況下將按升序排列。

此函式接受一個可選的比較函式,如果引數分別被認為小於、等於或大於第二個引數,則該函式返回一個小於、等於或大於零的整數。

語法

以下是 PHP Ds\Map::sorted() 函式的語法:

public Ds\Map Ds\Map::sorted([ callable $comparator ] )

引數

此函式接受一個作為比較器函式的單個可選引數,如下所述:

  • comparator - 比較函式返回一個整數值。

返回值

此函式返回當前地圖按值排序的副本。

示例 1

以下是 PHP Ds\Map::sorted() 函式的基本示例:

<?php 
   $map = new \Ds\Map([3, 5, 2, 1, 4]);
   echo "The map elements are: \n";
   print_r($map);
   echo "The sorted copy of a map is: \n";
   print_r($map->sorted());  
?>

輸出

上述程式產生以下輸出:

The map elements are:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 0
            [value] => 3
        )

    [1] => Ds\Pair Object
        (
            [key] => 1
            [value] => 5
        )

    [2] => Ds\Pair Object
        (
            [key] => 2
            [value] => 2
        )

    [3] => Ds\Pair Object
        (
            [key] => 3
            [value] => 1
        )

    [4] => Ds\Pair Object
        (
            [key] => 4
            [value] => 4
        )

)
The sorted copy of a map is:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 3
            [value] => 1
        )

    [1] => Ds\Pair Object
        (
            [key] => 2
            [value] => 2
        )

    [2] => Ds\Pair Object
        (
            [key] => 0
            [value] => 3
        )

    [3] => Ds\Pair Object
        (
            [key] => 4
            [value] => 4
        )

    [4] => Ds\Pair Object
        (
            [key] => 1
            [value] => 5
        )

)

示例 2

使用比較器函式。

以下是 PHP Ds\Map::sorted() 函式的另一個示例。我們使用此函式檢索此 ([50, 20, 40, 10, 30]) 地圖按值排序的副本,使用比較器函式:

<?php 
   $map = new \Ds\Map([50, 20, 40, 10, 30]);
   echo "The map elements are: \n";
   print_r($map);
   echo "The sorted copy of a map is: \n";
   #using comparator function
   $copy_map = $map->sorted(function($a, $b){
	   return $a<=>$b;
   });
   print_r($copy_map);
?>

輸出

執行上述程式後,它將顯示以下輸出:

The map elements are:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 0
            [value] => 50
        )

    [1] => Ds\Pair Object
        (
            [key] => 1
            [value] => 20
        )

    [2] => Ds\Pair Object
        (
            [key] => 2
            [value] => 40
        )

    [3] => Ds\Pair Object
        (
            [key] => 3
            [value] => 10
        )

    [4] => Ds\Pair Object
        (
            [key] => 4
            [value] => 30
        )

)
The sorted copy of a map is:
Ds\Map Object
(
    [0] => Ds\Pair Object
        (
            [key] => 3
            [value] => 10
        )

    [1] => Ds\Pair Object
        (
            [key] => 1
            [value] => 20
        )

    [2] => Ds\Pair Object
        (
            [key] => 4
            [value] => 30
        )

    [3] => Ds\Pair Object
        (
            [key] => 2
            [value] => 40
        )

    [4] => Ds\Pair Object
        (
            [key] => 0
            [value] => 50
        )

)
php_function_reference.htm
廣告