dtcd27183 2014-05-19 08:28
浏览 4

如何通过在php中操作两个数组来获取信息?

I have two array one for present list and other for total lectures. i want a third array that will hold the percent of two

My first array (present days list)

$allpresentAttInfo = array(
        0 => array(
           'year' => '2013',
           'term' => 'T1',
           'presentDays' => '123' 
        ),
        1 => array(
           'year' => '2013',
           'term' => 'T2',
           'presentDays' => '112'
        )
    );

My Second array (Total days list)

$allAttInfo = array(
        0 => array(
           'year' => '2013',
           'term' => 'T1',
           'totalDays' => '200' 
        ),
        1 => array(
           'year' => '2013',
           'term' => 'T2',
           'totalDays' => '216'
        )
    );

My Resultant array should be like this

 $attInfo = array(
        0 => array(
            'year' => '2013',
            'term' => 'T1',
            'presentPercent' => '63.7 %'
        ),
        1 => array(
            'year' => '2013',
            'term' => 'T2',
            'presentPercent' => '42.7 %'
        )
    );

So by merging both the arrays i will have to find the present present in given year and term. How to achieve this on PHP side. Thanks in advance

  • 写回答

1条回答 默认 最新

  • dongtingrun4973 2014-05-19 12:58
    关注

    Well thanks guys .. I was in a hurry so searching for some short-cut copy paste way ... Well i coded it. but still give me some performance optimization if available.

    My Solution is.

    $attInfo = array();
        foreach ($allAttInfo as $allatt) {
            foreach ($allpresentAttInfo as $allpre) {
                    if($allpre['year'] == $allatt['year'] && $allpre['term'] == $allatt['term']){
                       $presentPercent = round(($allpre['presentDays'] / $allatt['totalDay'])*100,2) . "%";
                       $newArray=array('year'=>$allpre['year'],'term'=>$allpre['term'],'presentPercent'=>$presentPercent);
                       array_push($attInfo, $newArray);
                }
            }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计