Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 比较单个数组中的n个值_Php_Loops_If Statement - Fatal编程技术网

Php 比较单个数组中的n个值

Php 比较单个数组中的n个值,php,loops,if-statement,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 +

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

   #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);
$final=collect();
dd(地点);
对于($i=0;$icategories);$j++){
如果($locations[$i]->name==$locations[$i+$j]->name){
//添加到最终集合
}
}
}
dd(最终);

我不完全理解您试图实现的目标,但是您可以尝试将
array\u unique()
array\u diff\u key()
相结合。这将为您提供数组中所有非唯一项的列表

比如:

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"
  }
}
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"
  }
}
而且不是唯一的回报:

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"
  }
}
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个实例的集合,并忽略单个条目。注意结果上的数组键。您可以利用这些优势。

这不是一个特别好的解决方案,因为它涉及到在数据上循环三次

$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);
下面是一个工作示例:


我注意到您使用的是Laravel,因此如果
$locations
位于
集合中,您可以使用可用的
集合方法来链接一些逻辑。

您的预期输出是什么?除了索引5处的位置之外的所有内容都应该在集合中?是的,这是正确的,基于这一小部分数据。整件事有83个条目。我不清楚这些类别的含义。你能举个例子说明你的意思吗?在上述示例中,如果只选择了两个类别,是否只会选择三个
Labadie Bauch
位置中的两个?这不清楚。数据太乱了,我将删除数组的上面部分,并发布一个更清晰的部分,并给出解释。@fubar我添加了一个不太乱的数据版本,因此基本上是因为搜索了最多两个类别,所以一个数据集应该能够返回的是位置的两个实例,因为这些类别是1和2,所以我返回带有类别1和类别2的位置的所有实例。我想做的是制作另一个集合,其中列出了所有同时包含这两个类别的位置,而不仅仅是一个。在这个数据集上,我需要返回
D'Amore Ltd
的两个实例。但这需要能够扩展,所以n个类别。感谢您的帮助!很抱歉反应太晚,我还没有时间测试你的解决方案,但逻辑看起来很合理,我相信它会引导我走向正确的方向。期中考试真的拖慢了我的发展。。