I have two different array. The array are like
first array is like
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
)
)
Second array is like
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
)
)
)
Now you can both array same key for source_langauge
and target_language
. The title
and file_name
key is available only in first array
So I want that the first array will search all the source_language and target_language from the second array and show both matching arrays and non matching arrays
So the output should be like this
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
)
as it has same values source language and target language in second array
The other result will show is
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
)
as this source language and target languages are not found in second array.
Update
I have tried so far is
$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;
}
}
But this one is not working at all
First off, you can't compare $h1
to $a2
since they don't share the exact same structure.
$h1
still has that title
and filename
key pair values.
No need to use md5
and json_encode
, since you just need to compare two string values:
$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;
}
}
<?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));
?>
The solution using array_walk
and array_diff
functions:
$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);
The output:
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
)
)