我有一个从mongoDB查询的元素数组。遍历数组并获得非重复值
该阵列具有设备的ID和设备消耗的值。
例如,有3个不同的ID - > 18,5,3和多个混合值。
// first record of 18 so get value.
$row[0]["id"] = 18;
$row[0]["value"] = 100;
// not first record of 18 so ignore and move to the next record
$row[1]["id"] = 18;
$row[1]["value"] = 40;
// first record of 5 so get value.
$row[2]["id"] = 5;
$row[2]["value"] = 20;
// not first record of 18 so ignore and move to the next record
$row[3]["id"] = 18;
$row[3]["value"] = 30;
// first record of 3 so get value.
$row[4]["id"] = 3;
$row[4]["value"] = 20;
//not first record of 5 so ignore and move to the next record**
$row[5]["id"] = 5;
$row[5]["value"] = 30;
// not first record of 18 so ignore and move to the next record
$row[6]["id"] = 18;
$row[6]["value"] = 10;
...
....
我想要做的是循环这个$行数组并获取最近的id值。
例如,在上面的例子我想回的是:
id value
18 100
5 20
3 20
我怎样才能做到这什么逻辑?
所以...如果你想循环...循环在哪里? – Dekel
你试过了吗? – Emaro
你提到5的第一个记录是20,但在你的回报值中,你放40。为什么? –