基于排列生成密钥的最佳算法
在我的应用程序中,我从 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解您想要正确执行的操作,那么这是我能想到的执行上述操作的最有效方法:
If I understand what you are trying to do correctly, this the most efficient method I can come up with for doing the above: