在PHP中比较两个数组,然后基于特定结构创建一个新数组



我想做这个数组:

(-,-,2,4,-,1,-,-,5)

使用数组 $ar 1 和 $ar 2:

$report[0]['progress'] = '2';
$report[1]['progress'] = '4';
$report[2]['progress'] = '1';
$report[3]['progress'] = '5';
$progress0 = $report[0]['progress'];
$progress1 = $report[1]['progress'];
$progress2 = $report[2]['progress'];
$progress3 = $report[3]['progress'];
$report[0]['month'] = 'Nov';
$report[1]['month'] = 'Dec';
$report[2]['month'] = 'Feb';
$report[3]['month'] = 'May';
$month0 = $report[0]['month'];
$month1 = $report[1]['month'];
$month2 = $report[2]['month'];
$month3 = $report[3]['month'];
$ar1 = array($progress0,$progress1,$progress2,$progress3);
$ar2 = array($month0,$month1,$month2,$month3);

最终数组将遵循以下格式(9月,10月,11月,12月,1月,2月,3月,4月,5月)因此,如果月份存在于 $ar 2 中,它将在 $ar 1 中显示相应的数字。如果月份不存在,则会显示 -。

因此,目标为 (-,-,2,4,-,1,-,-,5)

如何做到这一点?

更新的问题

为了简化起见,我正在尝试采取:

$ar1 = array(2,4,1,5);
$ar2 = array('Nov','Dec','Feb','May');

并使用此数组设置结构:

$ar3 = array('Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May')

在新数组中,将 $ar 2 中的月份替换为 $ar 1 中相同位置的数字,因此 $ar 2[2] 将变为 $ar 1[2],$ar 2 中不存在的月份将给出 -。

所以新阵列将变成

('-','-',2,4,'-',1,'-','-',5)

这应该会让你朝着正确的方向开始

$ar3 = array('Nov'=>'-', 'Sept'=>'-', ...);
for($i = 0; $i < count($ar1); $i++){
    $ar3[$ar2[$i]] = $ar1[$i]; 
}

最新更新