基于排列生成密钥的最佳算法

发布于 2024-12-05 03:24:11 字数 2121 浏览 1 评论 0原文

在我的应用程序中,我从 UI 表单中获得了一些参数(zone_id、startdate、enddate、brand、model)。其中每个参数都是一个包含 1 个或多个参数的数组。

我的表单参数看起来像这样:

Array
(
[sel_date_option] => today
[startdate] => 2011-09-19
[enddate] => 2011-09-19
[zone_id] => 1576,1562,1561
[model] => Array
    (
        [0] => A300
    )

[brand] => Array
    (
        [0] => ACTS
    )

)

现在,我想生成这些参数的组合键。它们会是这样的:

[zone_id]_[model]_[brand]_[model]_[startdate]_[enddate]

这反映了上述值的所有可能的排列。 对于上述输入,我应该得到以下键:

1576_ACTS_A300_2011-09-19
1562_ACTS_A300_2011-09-19
1561_ACTS_A300_2011-09-19

在给出相同的开始日期和结束日期一次的内部也可以输入一个时期,例如从 2011-09-19 到 2011-09-21。

我正在做的是,我使用嵌套循环遍历所有参数数组,然后创建一个复杂的数组数组,如下所示:

Array
(
[0] => Array
    (
        [0] => 1576_DZ_A300
    )

[1] => Array
    (
        [0] => 1576_DZ_A300
        [1] => 1562_DZ_A300
    )

[2] => Array
    (
        [0] => 1576_DZ_A300
        [1] => 1562_DZ_A300
        [2] => 1561_DZ_A300
    )

[3] => Array
    (
        [0] => 1576_DZ_A300
        [1] => 1562_DZ_A300
        [2] => 1561_DZ_A300
        [3] => 1563_DZ_A300
    )
 )

我正在做的是首先创建所有区域的数组:

Array
(
[0] => 1576
[1] => 1562
[2] => 1561
[3] => 1563
)

然后我使用 a 循环它所有可能的型号和品牌数组的嵌套循环如下所示:

function getInventoryData($criteria)
{

    $index = "";

    if($criteria['zone_id']!='')
    {
        $zone = explode(',', $criteria['zone_id']);
        for($i=0;$i<count($zone);$i++)
        {
            $index[] .= $zone[$i];
        }

        echo '<pre>';print_r($index);exit;
    }

    if(!empty($criteria['model']))
    {
        foreach($index as $key=>$value)
        {
            foreach($criteria['model'] as $model)
            {
                $temparr[] = $index[$key].'_'.$model;
            }
            $index[$key] = $temparr;
        }

    }

现在,还有其他有效的方法来实现这一点吗?

此外,还有与上述方法相关的另一个开销:

在读取生成的密钥时,我必须循环遍历所有级别,并且在数据序列较大的情况下,复杂性可能确​​实是一个值得关注的问题。

In my application , I have few parameters (zone_id, startdate, enddate, brand, model) which I get from my UI form . Each of these parameters is an array containing 1 or more parameters .

My form parameters looks something like this :

Array
(
[sel_date_option] => today
[startdate] => 2011-09-19
[enddate] => 2011-09-19
[zone_id] => 1576,1562,1561
[model] => Array
    (
        [0] => A300
    )

[brand] => Array
    (
        [0] => ACTS
    )

)

Now , I want to generate keys which are combinations of these parameters . They would be something like :

[zone_id]_[model]_[brand]_[model]_[startdate]_[enddate]

This reflect all the possible arrangements of the above values .
For the above inputs, I should get the following keys :

1576_ACTS_A300_2011-09-19
1562_ACTS_A300_2011-09-19
1561_ACTS_A300_2011-09-19

Inside of giving the same startdate and enddate once can also enter a period like from 2011-09-19 to 2011-09-21.

What I am doing is I am using nested looping through all the parameter array and then creating a complex array of arrays something like the following :

Array
(
[0] => Array
    (
        [0] => 1576_DZ_A300
    )

[1] => Array
    (
        [0] => 1576_DZ_A300
        [1] => 1562_DZ_A300
    )

[2] => Array
    (
        [0] => 1576_DZ_A300
        [1] => 1562_DZ_A300
        [2] => 1561_DZ_A300
    )

[3] => Array
    (
        [0] => 1576_DZ_A300
        [1] => 1562_DZ_A300
        [2] => 1561_DZ_A300
        [3] => 1563_DZ_A300
    )
 )

What I am doing is first I creating an array of all zones as :

Array
(
[0] => 1576
[1] => 1562
[2] => 1561
[3] => 1563
)

Then I am looping it using a nested loop for all the possible models and brands array like this :

function getInventoryData($criteria)
{

    $index = "";

    if($criteria['zone_id']!='')
    {
        $zone = explode(',', $criteria['zone_id']);
        for($i=0;$i<count($zone);$i++)
        {
            $index[] .= $zone[$i];
        }

        echo '<pre>';print_r($index);exit;
    }

    if(!empty($criteria['model']))
    {
        foreach($index as $key=>$value)
        {
            foreach($criteria['model'] as $model)
            {
                $temparr[] = $index[$key].'_'.$model;
            }
            $index[$key] = $temparr;
        }

    }

Now , is there any other efficient method to achieve this ?

Moreover , there is another overhead associated with the above method :

While reading the generated keys , I have to loop through all the levels and in case of large sequence of data , the complexity can be really a matter of concern.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请叫√我孤独 2024-12-12 03:24:11

如果我理解您想要正确执行的操作,那么这是我能想到的执行上述操作的最有效方法:

function getInventoryData ($arr) {
  // Make sure all required keys are set
  if (!isset($arr['zone_id'],$arr['model'],$arr['brand'],$arr['startdate'],$arr['enddate'])) return FALSE;
  // Make a date string for the end of the keys
  $dateStr = $arr['startdate'].(($arr['startdate'] == $arr['enddate']) ? '' : '_'.$arr['enddate']);
  // Normalise the data
  if (!count($arr['zone_id'] = array_unique(explode(',',$arr['zone_id']))) || !count($arr['brand'] = array_unique($arr['brand'])) || !count($arr['model'] = array_unique($arr['model']))) return FALSE;
  // Get all possible permutations
  $result = array();
  foreach ($arr['zone_id'] as $zone) foreach ($arr['brand'] as $brand) foreach ($arr['model'] as $model) $result[] = "{$zone}_{$brand}_{$model}_{$dateStr}";
  // Return the result
  return $result;
}

If I understand what you are trying to do correctly, this the most efficient method I can come up with for doing the above:

function getInventoryData ($arr) {
  // Make sure all required keys are set
  if (!isset($arr['zone_id'],$arr['model'],$arr['brand'],$arr['startdate'],$arr['enddate'])) return FALSE;
  // Make a date string for the end of the keys
  $dateStr = $arr['startdate'].(($arr['startdate'] == $arr['enddate']) ? '' : '_'.$arr['enddate']);
  // Normalise the data
  if (!count($arr['zone_id'] = array_unique(explode(',',$arr['zone_id']))) || !count($arr['brand'] = array_unique($arr['brand'])) || !count($arr['model'] = array_unique($arr['model']))) return FALSE;
  // Get all possible permutations
  $result = array();
  foreach ($arr['zone_id'] as $zone) foreach ($arr['brand'] as $brand) foreach ($arr['model'] as $model) $result[] = "{$zone}_{$brand}_{$model}_{$dateStr}";
  // Return the result
  return $result;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文