我有这个数组,但我想按照我的标准对其进行排序。
我想如果“country”和“top”相同,那么他们就是在一个地方。 稍微下面给出一个我的意思的例子。
$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
)
)
标准(必须匹配): 国家 最佳 并计算此数组中顶部的总和(总和)
如果有人知道如何处理这个问题,我将非常感激。提前谢谢。
答案 0 :(得分:1)
尝试以下代码。结果有很多修改。但我相信它会对你有用
$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);