查找当年总跨度与差距数组 [英] Find total year spans in an array with gaps

查看:114
本文介绍了查找当年总跨度与差距数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要画一个堆叠条形图此阵应该有2年期限(一个空格隔开年 2间酒吧1998年 2000

问题:
第一条应该是这样的,

  * 1998年至19​​97年*  -  * 2年的差距。*  -  * 2000-2008 *

在酒吧应该在喜欢它采取做 2000 ARRAY0 和<$ C合并较短的一年跨度$ C> 2008 从阵列1

 阵列

    [COMP_NAME] =&GT; C ++
    [parent_cat_name] =&GT;信息技术
    [sub_cat_name] =&GT;程序设计
    [total_years] =&GT; 6
    [last_year] =&GT; 2006年
    [START_YEAR] =&GT; 2000
)排列

    [COMP_NAME] =&GT; 。净
    [parent_cat_name] =&GT;信息技术
    [sub_cat_name] =&GT;程序设计
    [total_years] =&GT; 7
    [last_year] =&GT; 2008年
    [START_YEAR] =&GT; 2001年
)排列

    [COMP_NAME] =&GT; API
    [parent_cat_name] =&GT;信息技术
    [sub_cat_name] =&GT;程序设计
    [total_years] =&GT; 1
    [last_year] =&GT; 1998年
    [START_YEAR] =&GT; 1997年


解决方案

您想合并两个数组项,如果他们根据一些条件是相邻的,并有一些其他领域的一些平等的条件。

您可能会做:

 为(;;)
{
    $合并=阵列();
    $ N =计数($的数据);
    为($ I = 0;空($合并)及和放大器; $ I&LT; $ N-1; $ I ++)
    {
        为($ J = $ I + 1;空($合并)及和放大器; $ J&LT; $ N; $ J ++)
        {
            //为$ i和附加$ J全等?
            如果($数据[$ i] ['parent_cat_name']!= $数据[$ J] ['parent_cat_name'])
               继续;
            如果($数据[$ i] ['sub_cat_name']!= $数据[$ J] ['sub_cat_name'])
               继续;            // $是我和附加$ J相邻的?
            如果($数据[$ i] ['last_year'] + 1 == $数据[$ J] ['START_YEAR'])
            {
                $合并=阵列($ I,$ j)条;
                打破;
            }
            如果($数据[$ J] ['last_year'] + 1 == $数据[$ i] ['START_YEAR'])
                $合并=阵列(附加$ J,$ I);
                打破;
            }
            //它们是一致的,但不相邻,尝试下
        }
    }
    //如果我们到达终点,并发现什么可合并,退出。
    如果(空($合并))
        打破;
    列表($ I,$ j)条= $合并;
    //我们$ J新加至$ I
    $数据[$ i] ['last_year'] = $数据[$ J] ['last_year']
    $数据[$ i] ['total_years'] + = $数据[$ J] ['total_years']
    //我们摧毁附加$ J
    未设置($数据[$ J]);
    //重新编号脏
    $数据= array_values​​($的数据);
    //现在数据已被修改,让我们再次做到这一点。
}

I need to draw a stacked bar graph for this array which should have 2 bars separated with a white-space of 2 years span (from year 1998 to 2000)

The Problem: the first bar should be like,

*1998-1997* - *2 years gap*  - *2000-2008* 

The bars should merge shorter year-spans within like it did in taking 2000 from array0 and 2008 from array 1

Array
(
    [comp_name] => C++
    [parent_cat_name] => Information Technology
    [sub_cat_name] => Programming
    [total_years] => 6
    [last_year] => 2006
    [start_year] => 2000
)

Array
(
    [comp_name] => .NET
    [parent_cat_name] => Information Technology
    [sub_cat_name] => Programming
    [total_years] => 7
    [last_year] => 2008
    [start_year] => 2001
)

Array
(
    [comp_name] => API
    [parent_cat_name] => Information Technology
    [sub_cat_name] => Programming
    [total_years] => 1
    [last_year] => 1998
    [start_year] => 1997
)

解决方案

You want to merge two array items, if they are adjacent according to some condition AND have some equality condition on some other fields.

You might do:

for(;;)
{
    $merge = array();
    $n     = count($data);
    for ($i = 0; empty($merge) && $i < $n-1; $i++)
    {
        for ($j = $i+1; empty($merge) &&  $j < $n; $j++)
        {
            // Are $i and $j congruent?
            if ($data[$i]['parent_cat_name'] != $data[$j]['parent_cat_name'])
               continue;
            if ($data[$i]['sub_cat_name'] != $data[$j]['sub_cat_name'])
               continue;

            // Are $i and $j adjacent?
            if ($data[$i]['last_year']+1 == $data[$j]['start_year'])
            {
                $merge = array($i, $j);
                break;
            }
            if ($data[$j]['last_year']+1 == $data[$i]['start_year'])
                $merge = array($j, $i);
                break;
            }
            // They are congruent but not adjacent, try the next
        }
    }
    // If we get to the end and find nothing mergeable, exit.
    if (empty($merge))
        break;
    list($i, $j) = $merge;
    // We add $j to $i
    $data[$i]['last_year'] = $data[$j]['last_year']
    $data[$i]['total_years'] += $data[$j]['total_years']
    // We destroy $j
    unset($data[$j]);
    // Dirty renumber
    $data = array_values($data);
    // Now data has been modified, let's do this again.
}

这篇关于查找当年总跨度与差距数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆