我有两个不同的数组。数组就像
第一个数组就像
Array
(
[0] => Array
(
[source_language] => English
[target_language] => Array
(
[0] => German
[1] => Norwegian
)
[title] => file 1.xlsx
[file_name] => 1461911750_file_test.xlsx
)
[1] => Array
(
[source_language] => Hindi
[target_language] => Array
(
[0] => Belarusian
)
[title] => new files.xlsx
[file_name] => 1461912206_file_here_files.xlsx
)
)
第二个数组就像
Array
(
[1] => Array
(
[source_language] => English
[target_language] => Array
(
[0] => Urdu
[1] => Hindi
)
)
[2] => Array
(
[source_language] => Hindi
[target_language] => Array
(
[0] => Norwegian
)
)
[3] => Array
(
[source_language] => Hindi
[target_language] => Array
(
[0] => German
)
)
[3] => Array
(
[source_language] => English
[target_language] => Array
(
[0] => German
[1] => Norwegian
)
)
)
现在,您可以为source_langauge
和target_language
排列相同的密钥。 title
和file_name
键仅在第一个数组中可用
所以我希望第一个数组将从第二个数组中搜索所有source_language和target_language,并显示匹配的数组和非匹配的数组
所以输出应该是这样的
The matching array is
[0] => Array
(
[source_language] => English
[target_language] => Array
(
[0] => German
[1] => Norwegian
)
[title] => file 1.xlsx
[file_name] => 1461911750_file_test.xlsx
)
因为它在第二个数组中具有相同的源语言和目标语言
另一个结果将显示
non matching array is
[0] => Array
(
[source_language] => Hindi
[target_language] => Array
(
[0] => Belarusian
)
[title] => new files.xlsx
[file_name] => 1461912206_file_here_files.xlsx
)
因为在第二个数组中找不到此源语言和目标语言。
更新
到目前为止,我已经尝试了
$diffs = [];
foreach ($first_array as $a1) {
$h1 = md5(json_encode($a1));
$found = false;
foreach ($second_array as $a2) {
if (md5(json_encode($a2)) == $h1) {
$found = true;
break;
}
}
if ( !$found ) {
$diffs []= $a1;
}
}
但是这个根本不起作用
答案 0 :(得分:0)
首先,您无法将$h1
与$a2
进行比较,因为他们不会共享完全相同的结构。
$h1
仍然具有title
和filename
个密钥对值。
无需使用md5
和json_encode
,因为您只需要比较两个字符串值:
$match = $no_match = array();
foreach($first as $f) {
$found = false;
foreach($second as $s) {
if(
$f['source_language'] === $s['source_language'] &&
$f['target_language'] === $s['target_language']
) {
$match[] = $f;
$found = true;
}
}
if(!$found) {
$no_match[] = $f;
}
}
答案 1 :(得分:0)
<?php
$arr1 = array();
$arr2 = array();
$arr1[0] = array('source_language'=>'English','target_language'=>array('German','Norwegian'),'title'=>'file 1.xlsx','file_name'=>'1461911750_file_test.xlsx');
$arr1[1] = array('source_language'=>'Hindi','target_language'=>array('Belarusian'),'title'=>'new files.xlsx','file_name'=>'1461912206_file_here_files.xlsx');
$arr2[0] = array('source_language'=>'English','target_language'=>array('Urdu','Hindi'));
$arr2[1] = array('source_language'=>'Hindi','target_language'=>array('Norwegian'));
$arr2[2] = array('source_language'=>'Hindi','target_language'=>array('German'));
$arr2[3] = array('source_language'=>'English','target_language'=>array('German','Norwegian'));
$result_arr = array();
for($i=0;$i<count($arr2);$i++)
{
for($j=0;$j<count($arr1);$j++)
{
if(($arr2[$i]['source_language'] === $arr1[$j]['source_language']) && ($arr2[$i]['target_language'] === $arr1[$j]['target_language']))
$result_arr[] = $arr1[$j];
}
}
print_r(json_encode($result_arr));
?>
答案 2 :(得分:0)
使用array_walk
和array_diff
函数的解决方案:
$matched = $not_matched = [];
array_walk($array1, function($v) use($array2, &$matched, &$not_matched){
foreach ($array2 as $item) {
if ($v['source_language'] == $item['source_language']
&& empty(array_diff($v['target_language'], $item['target_language']))) {
$matched[] = $v;
break;
}
}
if (!in_array($v, $matched)) $not_matched[] = $v;
});
echo "Matched items:". PHP_EOL;
print_r($matched);
echo "Not-matched items:". PHP_EOL;
print_r($not_matched);
输出:
Matched items:
Array
(
[0] => Array
(
[source_language] => English
[target_language] => Array
(
[0] => German
[1] => Norwegian
)
[title] => file 1.xlsx
[file_name] => 1461911750_file_test.xlsx
)
)
Not-matched items:
Array
(
[0] => Array
(
[source_language] => Hindi
[target_language] => Array
(
[0] => Belarusian
)
[title] => files.xlsx
[file_name] => 1461912206_file_here_files.xlsx
)
)