PHP 数组按条件排序


PHP Array sort by criteria

我有这个数组,但我想按我的条件对其进行排序。

我希望如果"国家"和"顶部"相同,那么它们必须在一个地方。下面举个例子来说明我的意思。

$array = array(
    array("city" => "New York", "country" => "USA", "top" => "1"),
    array("city" => "London", "country" => "UK", "top" => "1"),
    array("city" => "Sofia", "country" => "BG", "top" => "1"),
    array("city" => "Belgrad", "country" => "SRB", "top" => "2"),
    array("city" => "Varna", "country" => "BG", "top" => "2"),
    array("city" => "LA", "country" => "UK", "top" => "1"),
    array("city" => "Bat", "country" => "USA", "top" => "1")
);

这是数组:

Array
(
    [0] => Array
        (
            [city] => New York
            [country] => USA
            [top] => 1
        )
    [1] => Array
        (
            [city] => London
            [country] => UK
            [top] => 1
        )
    [2] => Array
        (
            [city] => Sofia
            [country] => BG
            [top] => 1
        )
    [3] => Array
        (
            [city] => Belgrad
            [country] => SRB
            [top] => 2
        )
    [4] => Array
        (
            [city] => Varna
            [country] => BG
            [top] => 2
        )
    [5] => Array
        (
            [city] => LA
            [country] => USA
            [top] => 1
        )
    [6] => Array
        (
            [city] => Bat
            [country] => UK
            [top] => 1
        )
)

我想要这个结果:

Array
(
    [0] => Array
        (
            [0] => Array 
                (
                    [city] => New York
                    [country] => USA
                    [top] => 1
                )
            [1] => Array
                (
                    [city] => LA
                    [country] => USA
                    [top] => 1
                )
            [2] => Array
                (
                    [top_cities] => 2
                )
        )
    [1] => Array
        (
            [0] => Array 
                (
                    [city] => London
                    [country] => UK
                    [top] => 1
                )
            [1] => Array
                (
                    [city] => Bat
                    [country] => UK
                    [top] => 1
                )
            [2] => Array
                (
                    [top_cities] => 2
                )
        )
    [2] => Array
        (
            [0] => Array 
                (
                    [city] => Sofia
                    [country] => BG
                    [top] => 1
                )
        )
    [3] => Array
        (
            [city] => Belgrad
            [country] => SRB
            [top] => 2
        )
    [4] => Array
        (
            [city] => Varna
            [country] => BG
            [top] => 2
        )
)

条件(必须匹配):国家返回页首并计算此数组中的顶部(总和)

如果有人给出如何处理这个问题的想法,我将不胜感激。提前谢谢。

试试下面的代码。结果中有修改。但我相信它会为你工作

$array = array(
    array("city" => "New York", "country" => "USA", "top" => "1"),
    array("city" => "London", "country" => "UK", "top" => "1"),
    array("city" => "Sofia", "country" => "BG", "top" => "1"),
    array("city" => "Belgrad", "country" => "SRB", "top" => "2"),
    array("city" => "Varna", "country" => "BG", "top" => "2"),
    array("city" => "LA", "country" => "UK", "top" => "1"),
    array("city" => "Bat", "country" => "USA", "top" => "1")
);
$newArray = array();
foreach($array as $key => $val){
    $newArray[$val['country']][] = $val;
}
print_r($newArray);