Как сравнить два массива, игнорируя последние 4 символа, переменные во 2-м массиве?

Я пытаюсь сравнить два массива, но игнорировать последние четыре символа во втором массиве, вот два массива:

$a1 =array("Maths Foundation","Maths Higher","English Foundation","English Higher","English Lit Foundation","English Lit Higher");

$a2 = array("Maths Foundation 2015","Maths Higher 2014","English Foundation 2015","English Lit Higher 2014");

Я хочу, чтобы $ a2 смотрел на $ a1 и заменил соответствующие предметы и заменил ни одного совпадающего объекта на "". Таким образом, результат будет выглядеть так:

$a3 = array("Maths Foundation 2015","Maths Higher 2014","English Foundation 2015"," ","English Lit Higher 2014");

Таким образом, $ a3 заменит отсутствующий "Английский фонд Lit" на ""

////////////////////////////////////////////////////////////////////////////////////////////////////

Я думаю, что я близок, вот мой код:

$a1=array("Maths Foundation","Maths Higher","English Foundation","English Higher","English Lit Foundation","English Lit Higher");

$subjects = array("Maths Foundation 2015","Maths Higher 2014","English Foundation 2015","English Lit Higher 2014");
$subject = array();
$subjectyr = array();

foreach ($subjects as $v){
$yr = substr($v, -4);
$subb = substr($v, 0, -5);
$subject[] = ''.$subb.'';
$subjectyr[] = ''.$subb.' '.$yr.'';


$a3 = $a1;

$results = array_keys(array_diff($a1, $subjectyr));
foreach ($results as $result)
$a3[$result] = '';
}
$a4 = array_replace($a3,$subjectyr);
$string1 = implode(", ",$a4);
echo $string1;

/////////////////////РЕДАКТИРОВАТЬ//////////////////////////////////

Я решил попробовать еще одну тактику и ее приближение, но еще не все:

$a1=array("Maths Foundation","Maths Higher","English Foundation","English Higher","English Lit Foundation","English Lit Higher");

$a2 = array("Maths Foundation 2015","Maths Higher 2014","English Foundation 2015","English Lit Higher 2014");

foreach ($a1 as $a11){foreach ($a2 as $a22){
$a3 = substr($a22, 0, -5);
if ($a3 == $a11){
$thing[] = $a22;
}
else {
$thing[] = "";
}}}
print_r ($thing);
3 ответа

$a1 =array("Maths Foundation","Maths Higher","English Foundation","English Higher","English Lit Foundation","English Lit Higher");
$a2 = array("Maths Foundation 2015","Maths Higher 2014","English Foundation 2015","English Lit Higher 2014");

$temp = array();
$out = array();

foreach($a2 as $element)
 $temp[substr($element, 0, -5)]=$element;

foreach($a1 as $element)
 $out[]=isset($temp[$element])? $temp[$element] : ' ';

var_dump($out);

Выходы:

array(6) {
 [0]=>
 string(21) "Maths Foundation 2015"
 [1]=>
 string(17) "Maths Higher 2014"
 [2]=>
 string(23) "English Foundation 2015"
 [3]=>
 string(1) " "
 [4]=>
 string(1) " "
 [5]=>
 string(23) "English Lit Higher 2014"
}

http://codepad.viper-7.com/pDmoZd


Это должно сработать для вас:

<!--?php

 $a1 =array("Maths Foundation","Maths Higher","English Foundation","English Higher","English Lit Foundation","English Lit Higher");
 $a2 = array("Maths Foundation 2015","Maths Higher 2014","English Foundation 2015","English Lit Higher 2014");
 $a3 = array();

 foreach($a2 as $k1 =--> $v1) {

 foreach($a1 as $k2 => $v2) {

 if (substr($a2[$k1], 0, -5) == $a1[$k2]) {
 $a3[] = $a2[$k1];
 break;
 } elseif (count($a1) == ($k2+2))
 $a3[] = " ";

 }

 }

 var_dump($a3);

?>

Вывод:

array(5) {
 [0]=>
 string(21) "Maths Foundation 2015"
 [1]=>
 string(17) "Maths Higher 2014"
 [2]=>
 string(23) "English Foundation 2015"
 [3]=>
 string(2) " "
 [4]=>
 string(23) "English Lit Higher 2014"
}


Попробуй это:

$found = 0;
foreach ($a1 as $key => $value) {
 $found = 0;
 foreach ($subjects as $k => $v) {
 if ($value == $substr($v, 0, -5)) {
 array_push($subject, $v);
 $found = 1;
 break;
 }
 }
 if ($found == 0) {
 array_push($subject, "");
 }
}

licensed under cc by-sa 3.0 with attribution.