简体   繁体   English

将数组与多维数组进行比较以找到额外的元素

[英]Comparing an array to a multi-dimensional array to find an extra element

I want to do the following. 我想做以下事情。

Assume I have one array. 假设我有一个数组。

Array A, 阵列A,

-> [0] = A
-> [1] = B
-> [2] = C

That I want to compare to a multidimensional array 我想与多维数组进行比较

-> [0]
       [0] = 1
       [1] = A
       [2] = B
       [3] = C
-> [1]
       [0] = 2
       [1] = B
       [2] = C
       [3] = A

-> [2]
       [0] = 3
       [1] = C
       [2] = B
       [3] = A

I want to compare the first array, to the content in the multidimensional array. 我想比较第一个数组和多维数组中的内容。 As you can see the multidimensional array has a number attached to each combination of letters. 如您所见,多维数组的每个字母组合都附有一个数字。 I want to make the comparison in a way that I can get the corresponding number of array A out of array B, assuming that they're in array B too. 我希望以一种方式进行比较,我可以从数组B中获得相应数量的数组A,假设它们也在数组B中。 How can I do this? 我怎样才能做到这一点?

This should do it: 这应该这样做:

public function findSubArray($multiArr, $compArr) {
  foreach ($multiArr as $idx => $arr) { //for each sub array
    $first = array_shift($arr); //take off the first element and store it
    if (count(array_diff($arr, $compArr)) == 0) { //check if the remainder is what you want
      return $first; //if so, return the first element
    }
  } //otherwise loop
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM