我的数组结构如下-
[
[
"points" => 10,
"details" => ["name" => "Team A", "rank" => ""]
],
[
"points" => 10,
"details" => ["name" => "Team B", "rank" => ""]
],
[
"points" => 8,
"details" => ["name" => "Team C", "rank" => ""]
],
[
"points" => 6,
"details" => ["name" => "Team D", "rank" => ""]
],
]
现在我想用合适的密集秩填充数组的“秩”值。
[
[
"points" => 10,
"details" => ["name" => "Team A", "rank" => 1]
],
[
"points" => 10,
"details" => ["name" => "Team B", "rank" => 1]
],
[
"points" => 8,
"details" => ["name" => "Team C", "rank" => 2]
],
[
"points" => 6,
"details" => ["name" => "Team D", "rank" => 3]
],
]
我怎样才能得到这样的结果呢?我试着循环遍历数组中的每个元素并比较点,但是我没有发现这真的很有效。
3条答案
按热度按时间vddsk6oq1#
如何创建另一个数组并在其中存储所需的结果
输出将为:
ttp71kqs2#
有点蛮力,但应该可以。
5sxhfpxr3#
由于多维输入数组已经按
points
列降序排序,因此可以循环访问该数组并通过引用修改其rank
数据,同时有条件地递增秩变量。代码:(Demo)
上面的代码片段在
foreach()
中使用了"数组反结构化"来只声明使用过的变量;声明&$row
然后在循环体中访问其元素也是可行的。