在PHP中比较两个数组然后根据特定结构的新的数组? [英] In PHP compare two arrays then make a new array based on a specific structure?
问题描述
我想使这个数组:
( - , - ,2,4, - ,1, - , - ,5)
使用数组$ AR1和AR2 $
$报告[0] ['进步'] ='2';
$报告[1] ['进步'] ='4';
$报告[2] ['进步'] ='1';
$报告[3] ['进步'] ='5';
$ progress0 = $报告[0] ['进步'];
$ progress1 = $报告[1] ['进步'];
$ progress2 = $报告[2] ['进步'];
$ progress3 = $报告[3] ['进步'];$报告[0] ['月'] ='月';
$报告[1] ['月'] ='月';
$报告[2] ['月'] ='二月';
$报告[3] ['月'] ='五一';
$ month0 = $报告[0] ['月'];
$ MONTH1 = $报告[1] ['月'];
$ MONTH2 = $报告[2] ['月'];
$ month3 = $报告[3] ['月'];$ AR1 =阵列($ progress0,$ progress1,$ progress2,$ progress3);
$ AR2 =阵列($ month0,MONTH1 $,$ MONTH2,$ month3);
最后阵列将遵循格式(九月,十月,十一月,十二月,一月,二月,三月,四月,5月)
所以,如果每月在$ present AR2它会显示在$ AR1相应的数字。如果当月没有present它会显示一个 -
因此的目标( - , - ,2,4, - ,1, - , - ,5)
如何才能做到这一点?
已更新问题
要简化我试图采取:
$ AR1 =阵列(2,4,1,5);
$ AR2 =阵列('月','月','月','五一');
和使用这种阵列来设置结构:
$ AR3 =阵列('月','月','月','月','月','月','月','月','可能')
在一个新的阵列从$ AR1相同位置用数字代替从$ AR2的几个月里,所以$ AR2 [2]将成为$ AR1 [2],几个月不在$ AR2 present会给予 -
因此,新的阵列将成为
( - , - ,2,4, - ,1, - , - ,5)
这应该让你在正确的方向开始
$ AR3 =阵列('月'=>' - ','九月'=>' - ',...);
为($ I = 0; $ I<算(AR1 $); $ I ++){
$ AR3 [$ AR2 [$ i]] = $ AR1 [$ i];
}
I want to make this array:
(-,-,2,4,-,1,-,-,5)
using array $ar1 and $ar2:
$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);
The final array would follow the format (sep,oct,nov,dec,jan,feb,mar,apr,may) So if a month is present in $ar2 it would show the corresponding number in $ar1. If the month is not present it would show a -.
Hence the goal of (-,-,2,4,-,1,-,-,5)
How can this be done?
UPDATED QUESTION
To simplify I'm trying to take:
$ar1 = array(2,4,1,5);
$ar2 = array('Nov','Dec','Feb','May');
and using this array to set the structure:
$ar3 = array('Sep','Oct','Nov','Dec','Jan','Feb','Mar','Apr','May')
In a new array replace the months from $ar2 with numbers from the same locations in $ar1, so $ar2[2] would become $ar1[2], Months that are not present in $ar2 would be given a -.
So the new array would become
('-','-',2,4,'-',1,'-','-',5)
This should get you started in the right direction
$ar3 = array('Nov'=>'-', 'Sept'=>'-', ...);
for($i = 0; $i < count($ar1); $i++){
$ar3[$ar2[$i]] = $ar1[$i];
}
这篇关于在PHP中比较两个数组然后根据特定结构的新的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!