doucan8246326 2014-02-09 17:46
浏览 20
已采纳

在PHP中比较两个数组然后根据特定结构创建一个新数组?

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)
  • 写回答

1条回答 默认 最新

  • doumei7420 2014-02-09 18:59
    关注

    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]; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法