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
广告

© . All rights reserved.