Я пытаюсь сравнить два массива, но игнорирую последние четыре символа во втором массиве, вот два массива:
$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 заменит отсутствующий «English Lit Foundation» на «»
////////////////////////////////////////////////// //////////////////////////////////////////////////
Я думаю, что я близко, вот мой код до сих пор:
$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);
$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"}
Это должно работать для вас:
<?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, "");
}
}