通过匹配两个数组

时间:2017-12-14 12:44:33

标签: php arrays filtering

通过匹配两个数组来删除数组。一个是简单数组,另一个是多维数组。如果第一个数组中不存在值,我想删除第二个数组的元素。所以第一个数组中总共有20个元素。所以第二个数组中只有20个元素。

我想要的最终结果,其中value应与Ist数组匹配。第二个数组中没有元素,其值在第一个数组中不匹配。所以只有第二个数组的元素的值与第一个数组匹配。

           Array
(
[0] => Array
    (
        [label] => Afrikaans
        [value] => af
    )

[1] => Array
    (
        [label] => Albanian
        [value] => sq
    )

[2] => Array
    (
        [label] => Arabic
        [value] => ar
    )

[3] => Array
    (
        [label] => Armenian
        [value] => hy
    )

[4] => Array
    (
        [label] => Azerbaijani
        [value] => az
    )

 [5] => Array
    (
        [label] => Basque
        [value] => eu
     )

 [6] => Array
    (
        [label] => Belarusian
        [value] => be
    )

[7] => Array
    (
        [label] => Bengali
        [value] => bn
    )

[8] => Array
    (
        [label] => Bosnian
        [value] => bs
    )

[9] => Array
    (
        [label] => Bulgarian
        [value] => bg
    )

[10] => Array
    (
        [label] => Catalan
        [value] => ca
    )

[11] => Array
    (
        [label] => Cebuano
        [value] => ceb
    )

[12] => Array
    (
        [label] => Chinese
        [value] => zh-CN
    )

[13] => Array
    (
        [label] => Chinese (Traditional)
        [value] => zh-TW
    )

[14] => Array
    (
        [label] => Croatian
        [value] => hr
    )

[15] => Array
    (
        [label] => Czech
        [value] => cs
    )

[16] => Array
    (
        [label] => Danish
        [value] => da
    )

[17] => Array
    (
        [label] => Dutch
        [value] => nl
    )

[18] => Array
    (
        [label] => English
        [value] => en
    )

[19] => Array
    (
        [label] => Esperanto
        [value] => eo
    )

[20] => Array
    (
        [label] => Estonian
        [value] => et
    )

第一阵列

      Array
(
[0] => ar
[1] => bn
[2] => zh-CN
[3] => en
[4] => fr
[5] => de
[6] => el
[7] => gu
[8] => hi
[9] => it
[10] => ja
[11] => kn
[12] => ko
[13] => la
[14] => mr
[15] => pa
[16] => ru
[17] => es
[18] => ta
[19] => te
[20] => ur
)

第二阵列

   Array
(
[0] => Array
    (
        [label] => Afrikaans
        [value] => af
    )

[1] => Array
    (
        [label] => Albanian
        [value] => sq
    )

[2] => Array
    (
        [label] => Arabic
        [value] => ar
    )

[3] => Array
    (
        [label] => Armenian
        [value] => hy
    )

[4] => Array
    (
        [label] => Azerbaijani
        [value] => az
    )

 [5] => Array
    (
        [label] => Basque
        [value] => eu
     )

 [6] => Array
    (
        [label] => Belarusian
        [value] => be
    )

[7] => Array
    (
        [label] => Bengali
        [value] => bn
    )

[8] => Array
    (
        [label] => Bosnian
        [value] => bs
    )

[9] => Array
    (
        [label] => Bulgarian
        [value] => bg
    )

[10] => Array
    (
        [label] => Catalan
        [value] => ca
    )

[11] => Array
    (
        [label] => Cebuano
        [value] => ceb
    )

[12] => Array
    (
        [label] => Chinese
        [value] => zh-CN
    )

[13] => Array
    (
        [label] => Chinese (Traditional)
        [value] => zh-TW
    )

[14] => Array
    (
        [label] => Croatian
        [value] => hr
    )

[15] => Array
    (
        [label] => Czech
        [value] => cs
    )

[16] => Array
    (
        [label] => Danish
        [value] => da
    )

[17] => Array
    (
        [label] => Dutch
        [value] => nl
    )

[18] => Array
    (
        [label] => English
        [value] => en
    )

[19] => Array
    (
        [label] => Esperanto
        [value] => eo
    )

[20] => Array
    (
        [label] => Estonian
        [value] => et
    )

[21] => Array
    (
        [label] => Filipino
        [value] => tl
    )

[22] => Array
    (
        [label] => Finnish
        [value] => fi
    )

[23] => Array
    (
        [label] => French
        [value] => fr
    )

[24] => Array
    (
        [label] => Galician
        [value] => gl
    )

[25] => Array
    (
        [label] => Georgian
        [value] => ka
    )

[26] => Array
    (
        [label] => German
        [value] => de
    )

[27] => Array
    (
        [label] => Greek
        [value] => el
    )

[28] => Array
    (
        [label] => Gujarati
        [value] => gu
    )

[29] => Array
    (
        [label] => Haitian
        [value] => ht
    )

[30] => Array
    (
        [label] => Hausa
        [value] => ha
    )

[31] => Array
    (
        [label] => Hebrew
        [value] => iw
    )

[32] => Array
    (
        [label] => Hindi
        [value] => hi
    )

[33] => Array
    (
        [label] => Hmong
        [value] => hmn
    )

[34] => Array
    (
        [label] => Hungarian
        [value] => hu
    )

[35] => Array
    (
        [label] => Icelandic
        [value] => is
    )

[36] => Array
    (
        [label] => Igbo
        [value] => ig
    )

[37] => Array
    (
        [label] => Indonesian
        [value] => id
    )

[38] => Array
    (
        [label] => Irish
        [value] => ga
    )

[39] => Array
    (
        [label] => Italian
        [value] => it
    )

[40] => Array
    (
        [label] => Japanese
        [value] => ja
    )

[41] => Array
    (
        [label] => Javanese
        [value] => jv
    )

[42] => Array
    (
        [label] => Kannada
        [value] => kn
    )

[43] => Array
    (
        [label] => Khmer
        [value] => km
    )

[44] => Array
    (
        [label] => Korean
        [value] => ko
    )

[45] => Array
    (
        [label] => Lao
        [value] => lo
    )

[46] => Array
    (
        [label] => Latin
        [value] => la
    )

[47] => Array
    (
        [label] => Latvian
        [value] => lv
    )

[48] => Array
    (
        [label] => Lithuanian
        [value] => lt
    )

[49] => Array
    (
        [label] => Macedonian
        [value] => mk
    )

[50] => Array
    (
        [label] => Malay
        [value] => ms
    )

[51] => Array
    (
        [label] => Maltese
        [value] => mt
    )

[52] => Array
    (
        [label] => Maori
        [value] => mi
    )

[53] => Array
    (
        [label] => Marathi
        [value] => mr
    )

[54] => Array
    (
        [label] => Norwegian
        [value] => no
    )

[55] => Array
    (
        [label] => Persian
        [value] => fa
    )

[56] => Array
    (
        [label] => Polish
        [value] => pl
    )

[57] => Array
    (
        [label] => Portuguese
        [value] => pt
    )

[58] => Array
    (
        [label] => Punjabi
        [value] => pa
    )

[59] => Array
    (
        [label] => Romanian
        [value] => ro
    )

[60] => Array
    (
        [label] => Russian
        [value] => ru
    )

[61] => Array
    (
        [label] => Serbian
        [value] => sr
    )

[62] => Array
    (
        [label] => Slovak
        [value] => sk
    )

[63] => Array
    (
        [label] => Slovenian
        [value] => sl
    )

[64] => Array
    (
        [label] => Somali
        [value] => so
    )

[65] => Array
    (
        [label] => Spanish
        [value] => es
    )

[66] => Array
    (
        [label] => Swahili
        [value] => sw
    )

[67] => Array
    (
        [label] => Swedish
        [value] => sv
    )

[68] => Array
    (
        [label] => Tamil
        [value] => ta
    )

[69] => Array
    (
        [label] => Telugu
        [value] => te
    )

[70] => Array
    (
        [label] => Thai
        [value] => th
    )

[71] => Array
    (
        [label] => Turkish
        [value] => tr
    )

[72] => Array
    (
        [label] => Ukrainian
        [value] => uk
    )

[73] => Array
    (
        [label] => Urdu
        [value] => ur
    )

[74] => Array
    (
        [label] => Vietnamese
        [value] => vi
    )

[75] => Array
    (
        [label] => Welsh
        [value] => cy
    )

[76] => Array
    (
        [label] => Yiddish
        [value] => yi
    )

[77] => Array
    (
        [label] => Yoruba
        [value] => yo
    )

[78] => Array
    (
        [label] => Zulu
        [value] => zu
    )

  )

Example

3 个答案:

答案 0 :(得分:1)

$a1 = your first array;
$a2 = second array;

foreach($a2 as $k => $v)
{
    if(!in_array($v['value'],$a1)
    {
        unset($a2[$k]);
    }
}

因此,只有$a1中存在的唯一剩余值,您将获得$ a2。

答案 1 :(得分:0)

假设您不需要保留第二个数组中的索引,您可以翻转第一个数组并将临时键分配给第二个数组并调用array_intersect_key()

虽然这似乎会进行多次函数调用,但它们都不是在循环中完成的。在循环中使用in_array()将意味着(在您的情况下)php将执行59次完整阵列扫描和20次部分扫描 - 这将无效。

代码:(Demo

var_export(  // print to screen
    array_values(  // this is optional.  This just reindexes the array
        array_intersect_key(  // retain elements where key exists in both arrays
            array_column($fulllist,null,'value'),  // make new keys for 2nd array
            array_flip($keep)  // swap values and indexes of 1st array
        )
    )
);

输出:

array (
  0 => 
  array (
    'label' => 'Arabic',
    'value' => 'ar',
  ),
  1 => 
  array (
    'label' => 'Bengali',
    'value' => 'bn',
  ),
  2 => 
  array (
    'label' => 'Chinese',
    'value' => 'zh-CN',
  ),
  3 => 
  array (
    'label' => 'English',
    'value' => 'en',
  ),
  4 => 
  array (
    'label' => 'French',
    'value' => 'fr',
  ),
  5 => 
  array (
    'label' => 'German',
    'value' => 'de',
  ),
  6 => 
  array (
    'label' => 'Greek',
    'value' => 'el',
  ),
  7 => 
  array (
    'label' => 'Gujarati',
    'value' => 'gu',
  ),
  8 => 
  array (
    'label' => 'Hindi',
    'value' => 'hi',
  ),
  9 => 
  array (
    'label' => 'Italian',
    'value' => 'it',
  ),
  10 => 
  array (
    'label' => 'Japanese',
    'value' => 'ja',
  ),
  11 => 
  array (
    'label' => 'Kannada',
    'value' => 'kn',
  ),
  12 => 
  array (
    'label' => 'Korean',
    'value' => 'ko',
  ),
  13 => 
  array (
    'label' => 'Latin',
    'value' => 'la',
  ),
  14 => 
  array (
    'label' => 'Marathi',
    'value' => 'mr',
  ),
  15 => 
  array (
    'label' => 'Punjabi',
    'value' => 'pa',
  ),
  16 => 
  array (
    'label' => 'Russian',
    'value' => 'ru',
  ),
  17 => 
  array (
    'label' => 'Spanish',
    'value' => 'es',
  ),
  18 => 
  array (
    'label' => 'Tamil',
    'value' => 'ta',
  ),
  19 => 
  array (
    'label' => 'Telugu',
    'value' => 'te',
  ),
  20 => 
  array (
    'label' => 'Urdu',
    'value' => 'ur',
  ),
)

答案 2 :(得分:-1)

容易(如果我设法理解......)

let newArray = languages.filter(lang => return codes.indexof(lang.value) !== -1);

<强>更新

哦,我的:-)错误的语言,抱歉

$final = array_filter($languages, function($language) use($codes) { return in_array($language['value'], $codes)});

更新2

让我把它踩下来:

array_filter可用于过滤数组:-),默认情况下,array_filter将删除&#34; falsy&#34;值。使用回调作为第二个参数,您可以定义如何过滤输入数组。第一个参数是输入数组。

我使用array_filter而不是取消设置原始值,因为它更干净,更健壮。

PHP知道匿名函数 - 过滤器操作是一个完美的用例。但是因为PHP不是最好的编程语言,我们必须使用特殊的语法来模拟某种范围,这就是为什么你会看到这个部分:

use($codes)

如果我要定义过滤器,如:

$final = array_filter($languages, function($language) {
    return in_array($language['value'], $codes)
});

PHP会抛出异常,因为$ code未在函数范围内定义。

匿名函数支持use()块,这样我们可以&#34;暴露&#34;代码数组到我们的函数。

$final = array_filter($languages, function($language) use($codes) {
    return in_array($language['value'], $codes)
});

最后,我们通过搜索$ codes数组中的语言值来检查语言是否有效(它将保存允许的语言)。如果$ code中的代码不存在,则in_array将返回false并将其返回给array_filter,因此该元素无效。

如果代码中存在检查语言的代码,in_array将返回true,我们将其返回到array_filter并且该项有效。