【问题标题】:Comparing two arrays in PHP in random order以随机顺序比较PHP中的两个数组
【发布时间】:2019-04-27 02:15:10
【问题描述】:

我需要查看一个数组中的任何值是否存在于另一个数组中,并且数字是随机顺序的。当值的顺序​​不完全相同时,我发现解决方案似乎不起作用。

我尝试过array_intersect,但如果我要查找的号码不在同一顺序,这将不起作用。

$array1 = [1,2];
$array2 = [2,3];

$result = array_intersect($array1, $array2);

$result 返回 false,但我希望它意识到两个数组中都存在 2 并返回 true。

我想这有一个简单的解决方案,但找不到任何可行的方法。

更新:

这是完整的代码(使用 PHP,Laravel):

$student = User::find($id);
$studentLocations = $student->hospital()->pluck('id');
$preceptorLocations = Auth::user()->hospital()->pluck('id');

$result = array_intersect($studentLocations, $preceptorLocations);

if if 返回每个的结果:

[2] // studentLocations
[1,2] // preceptorLocations

但是,上面的完整代码我得到:

"array_intersect(): Argument #1 is not an array"

例如,如果我更改为array($student->hospital()->pluck('id')),它不会出错,不会返回true,当我只返回结果时,它们是这样的:

[[2]]

【问题讨论】:

  • 您的代码运行良好。 3v4l.org/a7GKI
  • 当我使用 if 语句时:'code' if ($result) { // do stuff } 'code' 它不起作用。
  • 您发布的代码运行良好。如果您的代码确实有问题,请编辑您的帖子以包含该代码。
  • if(count($result)>0) ?
  • 请提取minimal reproducible example。鉴于最近的编辑,您的问题也完全改变了,这表明前期准备不足。请接受tour 并阅读How to Ask 以获得指导!

标签: php arrays laravel


【解决方案1】:

检查预期的 id 是否在您的数组中:

if(in_array($studentLocations, $preceptorLocations)){
    //Your code
}

in_array() 函数检查数组中是否存在值。 假设 $studentsLocations 是一个整数。如果是数组

if (!array_diff($studentLocations, $preceptorLocations)) {
    //Your code
};

array_diff 计算数组的差异。

--- 更新 ---

您可以使用 laravel intersect() 方法检查第一个集合的某些元素是否存在于第二个集合中。在这种情况下,count函数的返回值大于0。

if (count($studentLocations->intersect($preceptorLocations))>0) {
        //Your code here
} 

Laravel 5.8 Collections Documentation

【讨论】:

  • 有时它们都是数组,可以是 [1,2] 和 [2,3]。上面的代码在这种情况下不起作用。
【解决方案2】:
$array1 = [1,2];
$array2 = [2,3];

$result = array_intersect($array1, $array2);
print_r($result);

这会打印出 Array([1] => 2),因为键与 array_intersect 一起保存

如果你提取值,你会得到一个没有保留键的结果

$result = array_values(array_intersect($array1, $array2));
print_r($result);

这将打印 Array([0] => 2)

【讨论】:

  • 我不需要这些值,当相同的数字出现在两个不同的数组中时(但以随机顺序),我需要一个“真”。这行不通。我仍然收到一个错误,即参数 1 不是数组,即使我单独返回参数,它们都打印为 [1,2] 或 [2,3]。
【解决方案3】:

您可能会检查您的 PHP 脚本,并且您可能留下了一个可能会给您带来其他错误的字符。这有效:

$array1 = [1, 12, 9, 101, 2, 22, 14];
$array2 = [2, 3, 10, 22, 14, 0];

if (is_array($array1) && is_array($array1)) {
    $result = array_intersect($array1, $array2);

    if (sizeof($result) > 0) {
        $output = "\n";
        foreach ($result as $value) {
            $output .= 'Value ' . $value . " is in both arrays.\n";
        }
        echo $output;
    } else {
        echo "Sorry! There is no result!\n";
    }

}else{
    echo "Sorry! One or both of inputs are not in array form!"
}

输出:

string(84) "
Value 2 is in both arrays.
Value 22 is in both arrays.
Value 14 is in both arrays.
"

【讨论】:

  • 太奇怪了,我仍然得到“参数 1 不是数组”。但是当我将任一数组返回到页面时,它们是 [1,2] 或 [2,3]。如果我将它们包装在 array($values) 中,它不会返回该错误,但它也不会返回存在匹配值的 true。
  • 当我在我的控制器中var_dump($studentLocations);(使用 Laravel)时,它被忽略了。如果我return $studentLocations 我得到[2,3]
  • echo 在控制器中被忽略,但如果我 return $is_array1return @is_array2 我得到错误 "The Response content must be a string or object implementing __toString(), "boolean" given."
  • 感谢@Emma,不幸的是,控制器中忽略了 var_dump(我不知道为什么),所以答案不适用于我的 Laravel 控制器。我感谢您的帮助。我需要睡觉,也许它会在早上工作 ;-)。
  • 在你建议之前,我从未在 Laravel 中尝试过 var_dump,而 echo 在控制器中没有做任何事情。在您将值返回到视图之前,控制器会建立所有后端内容。但是如果你在查看之前return $values,它只会将它们转储到浏览器页面上。
【解决方案4】:

我的原始帖子遇到了这个问题,我用基本术语描述了这个问题,然后用实际代码进行了描述。

$student = User::find($id);
$studentLocations = $student->hospital()->pluck('id');
$preceptorLocations = Auth::user()->hospital()->pluck('id');

$result = array_intersect($studentLocations, $preceptorLocations);

解决方案是,在 Laravel 中,pluck 返回一个对象。通过在变量末尾添加toArray(),它现在可以完美运行。

$student = User::find($id);
$studentLocations = $student->hospital()->pluck('id')->toArray();
$preceptorLocations = Auth::user()->hospital()->pluck('id')->toArray();

$result = array_intersect($studentLocations, $preceptorLocations);

【讨论】:

    【解决方案5】:

    我猜这是因为 laravel 给了你collection,因此它不是一个数组而是一个Collection,所以你没有得到你想要的结果。 所以首先将您的collection 转换为array 然后将该数组传递给您的function 对于转换,您可以使用集合的toArray()

    $studentLocations = $student->hospital()->pluck('id')->toArray();
    $preceptorLocations = Auth::user()->hospital()->pluck('id')->->toArray();
    

    它应该可以工作。

    【讨论】:

    • 是的,我刚刚在您发表评论前 5 分钟发布了这个答案,您是对的。我终于想通了。
    • 我猜我们正在同时打字。检查语法需要时间;)
    【解决方案6】:

    您可以通过在查询中使用whereIn() 来完全避免使用array_intersect。

    $student = User::find($id);
    $studentLocations = $student->hospital()->pluck('id')->toArray();
    $result = Auth::user()->hospital()->whereIn('id',$studentLocations)->pluck('id')->toArray();
    

    【讨论】:

      猜你喜欢
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 2021-01-24
      • 2013-03-21
      • 2015-03-31
      • 1970-01-01
      • 2020-12-10
      相关资源
      最近更新 更多