比较单个数组中的n个值

时间:2018-10-10 01:22:54

标签: php loops if-statement

所以我有一个关联数组,如下所示:

   #items: array:84 [
0 => {#513
  +"name": "Spencer, Heaney and Von"
  +"id": 49
  +"lon": 40.68
  +"lat": 90.25
  +"catId": 2
  +"locId": 49
  +"distance": 48.796277782319
}
1 => {#514
  +"name": "Yost-Terry"
  +"id": 16
  +"lon": 40.07
  +"lat": 90.99
  +"catId": 2
  +"locId": 16
  +"distance": 52.598218030179
}
2 => {#515
  +"name": "Mills-Okuneva"
  +"id": 98
  +"lon": 40.84
  +"lat": 89.79
  +"catId": 1
  +"locId": 98
  +"distance": 59.083838249165
}
3 => {#516
  +"name": "D'Amore Ltd"
  +"id": 67
  +"lon": 40.88
  +"lat": 89.82
  +"catId": 1
  +"locId": 67
  +"distance": 61.538239638491
}
4 => {#517
  +"name": "D'Amore Ltd"
  +"id": 67
  +"lon": 40.88
  +"lat": 89.82
  +"catId": 2
  +"locId": 67
  +"distance": 61.538239638491
}
5 => {#518
  +"name": "Kuvalis, Denesik and Terry"
  +"id": 14
  +"lon": 41.03
  +"lat": 90.05
  +"catId": 1
  +"locId": 14
  +"distance": 71.218957454573
}

现在,我要尝试的是遍历每个位置,并检查其后的值是否相同,或者接下来的两个值是否相同,一直到接下来的n个值都相同。

因此,基本上,如果用户输入了4个类别,则它将采用第一个地点名称,然后检查接下来的3个地点名称,看看它们是否相同,如果是的话,它将把具有该名称的地点的所有实例添加到集合中

(请注意,这是伪造的数据,因此,“ Labadie-Bauch”位置在实时数据的所有4个条目中都不会具有相同的catId)

以下代码是我试图解决的尝试,但没有用。我相信,如果用户仅通过2个类别,而用户通过n个类别,则不行。

$final = collect();

dd($locations);
for($i = 0; $i < count($locations);$i++){
    for($j = 1; $j < count($request->categories);$j++){
        if($locations[$i]->name == $locations[$i+$j]->name){
            //add to final collection
        }
    }
}
dd($final);

2 个答案:

答案 0 :(得分:1)

我不太了解您要实现的目标,但是您可以尝试将array_unique()array_diff_key()结合使用。这将为您提供数组中所有NON唯一条目的列表。

类似的东西:

array = array(
    1 => array('a', 'b', 'c', 'd'), 
    3 => array('a', 'b', 'c', 'd'), 
    4 => array('a', 'b', 'c', 'd'), 
    5 => array('d', 'e', 'f', 'g'), 
    6 => array('d', 'e', 'f', 'g'),
    7 => array('h', 'i', 'j', 'k')
);

$unique = array_unique($array, SORT_REGULAR);
$not_unique = array_diff_key($array, $unique);

独特的回报:

array(3) {
  [1]=>
  array(4) {
    [0]=>
    string(1) "a"
    [1]=>
    string(1) "b"
    [2]=>
    string(1) "c"
    [3]=>
    string(1) "d"
  }
  [5]=>
  array(4) {
    [0]=>
    string(1) "d"
    [1]=>
    string(1) "e"
    [2]=>
    string(1) "f"
    [3]=>
    string(1) "g"
  }
  [7]=>
  array(4) {
    [0]=>
    string(1) "h"
    [1]=>
    string(1) "i"
    [2]=>
    string(1) "j"
    [3]=>
    string(1) "k"
  }
}

and not_unique返回:

array(3) {
  [3]=>
  array(4) {
    [0]=>
    string(1) "a"
    [1]=>
    string(1) "b"
    [2]=>
    string(1) "c"
    [3]=>
    string(1) "d"
  }
  [4]=>
  array(4) {
    [0]=>
    string(1) "a"
    [1]=>
    string(1) "b"
    [2]=>
    string(1) "c"
    [3]=>
    string(1) "d"
  }
  [6]=>
  array(4) {
    [0]=>
    string(1) "d"
    [1]=>
    string(1) "e"
    [2]=>
    string(1) "f"
    [3]=>
    string(1) "g"
  }
}

您可以组合两个数组以获取N个实例的集合,而忽略单个条目。注意结果上的数组KEYS。您可以利用这些优势。

答案 1 :(得分:1)

这不是一个特别漂亮的解决方案,因为它涉及遍历数据三次。

$locations = [/* ... */];
$categories = [1, 2];
$collection = [];

// Reduce locations to an array categories, grouped by location name
$grouped = array_reduce($locations, function ($grouped, $location) {
    $grouped[$location->name][] = $location->catId;
    return $grouped;
}, []);

// Filter the location groups to remove any locations that do not match all categories
$grouped = array_filter($grouped, function ($_categories) use ($categories) {
    return ! array_diff($categories, $_categories);
});

// Append locations with matching name and category to collection
foreach ($locations as $location) {
    if (isset($grouped[$location->name]) && in_array($location->catId, $categories)) {
        $collection[] = $location;
    }
}


var_dump($collection);

这是一个有效的示例:https://3v4l.org/mBZZn

我注意到您正在使用Laravel,因此,如果$locationsCollection中,则可以使用可用的Collection方法来链接其中的某些逻辑。 / p>