循环遍历数组并获取非重复值

时间:2017-09-15 14:10:37

标签: php

我从mongoDB查询了一系列元素。

此数组具有设备的ID和此设备的消耗值。

例如,有3种不同的ID - > 18,5,3和多个混合值。

arr=[]
n=int(input("Enter the number of terms you want in the array"))


for i in range(0,n):
      a=int(input("Enter your number here"))
      arr.append(a)

for i in range(0,len(arr)):
    if arr[i]==arr[len(arr)-i-1]:
        print("The "+i+"th element and the "+len(arr)-i-1+"th element are equal" )

我要做的是遍历这个$ row数组并获取id的最新值。

例如,在上面的示例中,我想要返回的是:

// 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;


...
....

我怎么能做那种逻辑?

5 个答案:

答案 0 :(得分:1)

试试这个

Array
(
    [0] => Array
        (
            [id] => 18
            [value] => 100
        )

    [1] => Array
        (
            [id] => 5
            [value] => 20
        )

    [2] => Array
        (
            [id] => 3
            [value] => 20
        )

)

结果

.bg-image{
  height: 300px;
  width: 300px;
  background-color: red;  
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center; 
  display: inline-block;
}

答案 1 :(得分:1)

如果您只想保留每个' id'然后只需将值添加到聚合数组中 - 但前提是它们尚未添加。然后获取聚合数组的值。

https://tehplayground.com/NRvw9uJF615oeh6C - 按Ctrl + Enter运行

$results = array();
foreach ($row as $r) {
    $id = $r['id'];
    if (! array_key_exists($id, $results)) {
        $results[$id] = $r;
    }
}

$results = array_values($results);
print_r($results);
Array
(
    [0] => Array
        (
            [id] => 18
            [value] => 100
        )

    [1] => Array
        (
            [id] => 5
            [value] => 20
        )

    [2] => Array
        (
            [id] => 3
            [value] => 20
        )

)

答案 2 :(得分:1)

可以通过多种方式完成。最简单的方法是使用foreach

$result = array();
foreach ($row as $i) {
    if (! array_key_exists($i['id'], $result)) {
        $result[$i['id']] = $i['value'];
    }
}

# Verify the result
print_r($result);

输出结果为:

Array
(
    [18] => 100
    [5] => 20
    [3] => 20
)

相同的处理,但使用array_reduce()

$result = array_reduce(
    $row,
    function(array $c, array $i) {
        if (! array_key_exists($i['id'], $c)) {
            $c[$i['id']] = $i['value'];
        }
        return $c;
    },
    array()
);

答案 3 :(得分:0)

array_unique()函数正是您所关注的。 请参阅此处的文档:array_unique() documentation

答案 4 :(得分:0)

Using array_column with an index key will almost do it, but it will be in the reverse order, so you can reverse the input so that it works.

$result = array_column(array_reverse($row), 'value', 'id');