我需要根据如何构建它们的说明创建一个大的部件号列表。 这是两个例子。
每个部件编号分为如下部分:0-1-2-3-4-5-6,某些部件具有依赖关系,其中,根据先前部件的值,对该特定部件进行限制选择。
这是我到目前为止的php。我有一个包含模式的数组,但我很难创建输出所有选项的循环:
$parts[0] = array('APXE');
$parts[1] = array('N','P',"S");
$parts[2] = array('300','400','600','700','800','900','1G4','1G8','2G0','2G5');
$parts[3] = array(
array('dependIndex'=>'2','dependVal'=>'300','values'=>array('025','050','075','150','200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'400','values'=>array('025','050','075','150','200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'600','values'=>array('1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'700','values'=>array('200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'800','values'=>array('075','150','200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'900','values'=>array('025','050','075','150','200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'1G4','values'=>array('075','150','250','500','1M0','1M7','3M5','7M0')),
array('dependIndex'=>'2','dependVal'=>'1G8','values'=>array('250','500','1M0','1M7','3M5','7M0','14M')),
array('dependIndex'=>'2','dependVal'=>'2G0','values'=>array('250','500','1M0','1M7','3M5','7M0','14M')),
array('dependIndex'=>'2','dependVal'=>'2G5','values'=>array('250','500','1M0','1M7','3M5','7M0','14M'))
);
$parts[4] = array(
array('dependIndex'=>'2','dependVal'=>'300','values'=>array('A0','A1','A2')),
array('dependIndex'=>'2','dependVal'=>'400','values'=>array('B0','B1','B2','C0')),
array('dependIndex'=>'2','dependVal'=>'600','values'=>array('D0')),
array('dependIndex'=>'2','dependVal'=>'700','values'=>array('E0')),
array('dependIndex'=>'2','dependVal'=>'800','values'=>array('F0')),
array('dependIndex'=>'2','dependVal'=>'900','values'=>array('G0','G2','G3','G4')),
array('dependIndex'=>'2','dependVal'=>'1G4','values'=>array('H0','H1')),
array('dependIndex'=>'2','dependVal'=>'1G8','values'=>array('K0')),
array('dependIndex'=>'2','dependVal'=>'2G0','values'=>array('I0')),
array('dependIndex'=>'2','dependVal'=>'2G5','values'=>array('J0','J1'))
);
$parts[5] = array('12','1L','24','48','AC');
$parts[6] = array('ET','FC','IC','FI','NT');
$parts[7] = array('OO');
foreach($parts as $part)
foreach ($part as $bit){
if(!is_array($bit) $list[] = $bit;
else foreach($bit['values'] as $val) $list[] = $val;
} //??
在上面的'dependIndex'=>'2'中,'dependVal'=>'300'表示对于第二段中具有'300'的所有部件号,第三段可以具有该数组的所有值。
我正在寻找一个足够灵活的PHP函数,以便在有不同数量的段时处理它。依赖关系也会改变。所以在上面的例子中,4可能取决于3和6可能取决于1。
有人可以说递归吗?大声笑
非常感谢任何帮助。
答案 0 :(得分:0)
首先,我认为通过使用从数组中选择一个或多个随机条目的array_rand()
来生成一个零件号会很好。稍后您可以修改代码以使用递归或嵌套foreach
来输出所有可能的部分。
foreach ($parts as $part)
{
if (!is_array($part[0]))
$list[] = array_rand($part);
else
{
$val = "UNKNOWN";
// Handle dependencies by looking back to the relevant section of
// the current part number that is being generated.
foreach ($part as $bit)
if ($list[$bit['dependIndex']] == $bit['dependVal'])
$val = array_rand($bit['values']);
$list[] = $val;
}
}
var_dump($list);
生成所有可能的部件是一个相当难的问题。如果这是一个家庭作业问题,你的教授必须对你充满信心!
答案 1 :(得分:0)
您正在寻找两种功能。一个用于解析依赖关系,另一个用于生成数字。
鉴于以下模型中的依赖关系:
$dependencies = array(
'300'=>array(
0=>array('025','050','075','150','200','250','500','1M0','1M7','3M5'),
1=>array('A0','A1','A2')
),
'400'=>array(
0=>array('025','050','075','150','200','250','500','1M0','1M7','3M5'),
1=>array('B0','B1','B2','C0')
),
'600'=>array(
0=>array('1M7','3M5'),
1=>array('D0')
),
'700'=>array(
0=>array('200','250','500','1M0','1M7','3M5'),
1=>array('E0')
),
'800'=>array(
0=>array('075','150','200','250','500','1M0','1M7','3M5'),
1=>array('F0')
),
'900'=>array(
0=>array('025','050','075','150','200','250','500','1M0','1M7','3M5'),
1=>array('G0','G2','G3','G4')
),
'1G4'=>array(
0=>array('075','150','250','500','1M0','1M7','3M5','7M0'),
1=>array('H0','H1')
),
'1G8'=>array(
0=>array('250','500','1M0','1M7','3M5','7M0','14M'),
1=>array('H0','H1')
),
'2G0'=>array(
0=>array('250','500','1M0','1M7','3M5','7M0','14M'),
1=>array('I0')
),
'2G5'=>array(
0=>array('250','500','1M0','1M7','3M5','7M0','14M'),
1=>array('J0','J1')
)
);
您可以创建一个将位置3,4和5组合成预先计算的字符串的函数:
function position3($arrays){
$retVal = array();
foreach ($arrays as $key=>$value){
foreach ($value[0] as $v1){
foreach ($value[1] as $v2){
array_push($retVal, $key . '-' . $v1 . '-' . $v2);
}
}
}
return $retVal;
}
将位置3组合在所有已定义的突变中,您可以将位置映射到数组模型中,如下所示:
$parts[0] = array('APXE');
$parts[1] = array('N','P',"S");
$parts[2] = position3($dependencies);
$parts[3] = array('12','1L','24','48','AC');
$parts[4] = array('ET','FC','IC','FI','NT');
$parts[5] = array('OO');
然后,您可以调用一个循环函数,使用此函数构建组合值的每个排列(总共13275):
function go($parts){
$total = 0;
foreach ($parts[0] as $pos1){
foreach ($parts[1] as $pos2){
foreach ($parts[2] as $pos3){
foreach ($parts[3] as $pos4){
foreach ($parts[4] as $pos5){
foreach ($parts[5] as $pos6 ){
echo implode('-', [$pos1,$pos2,$pos3,$pos4,$pos5,$pos6 ])."\n";
$total++;
}
}
}
}
}
}
echo $total;
}
将所有这些放在一起将使:
function position3($arrays){
$retVal = array();
foreach ($arrays as $key=>$value){
foreach ($value[0] as $v1){
foreach ($value[1] as $v2){
#echo $key . '-' . $v1 . '-' . $v2 . "\n";
array_push($retVal, $key . '-' . $v1 . '-' . $v2);
}
}
}
return $retVal;
}
$dependencies = array(
'300'=>array(
0=>array('025','050','075','150','200','250','500','1M0','1M7','3M5'),
1=>array('A0','A1','A2')
),
'400'=>array(
0=>array('025','050','075','150','200','250','500','1M0','1M7','3M5'),
1=>array('B0','B1','B2','C0')
),
'600'=>array(
0=>array('1M7','3M5'),
1=>array('D0')
),
'700'=>array(
0=>array('200','250','500','1M0','1M7','3M5'),
1=>array('E0')
),
'800'=>array(
0=>array('075','150','200','250','500','1M0','1M7','3M5'),
1=>array('F0')
),
'900'=>array(
0=>array('025','050','075','150','200','250','500','1M0','1M7','3M5'),
1=>array('G0','G2','G3','G4')
),
'1G4'=>array(
0=>array('075','150','250','500','1M0','1M7','3M5','7M0'),
1=>array('H0','H1')
),
'1G8'=>array(
0=>array('250','500','1M0','1M7','3M5','7M0','14M'),
1=>array('H0','H1')
),
'2G0'=>array(
0=>array('250','500','1M0','1M7','3M5','7M0','14M'),
1=>array('I0')
),
'2G5'=>array(
0=>array('250','500','1M0','1M7','3M5','7M0','14M'),
1=>array('J0','J1')
)
);
$parts[0] = array('APXE');
$parts[1] = array('N','P',"S");
$parts[2] = position3($dependencies);
$parts[3] = array('12','1L','24','48','AC');
$parts[4] = array('ET','FC','IC','FI','NT');
$parts[5] = array('OO');
function go($parts){
$total = 0;
foreach ($parts[0] as $pos1){
foreach ($parts[1] as $pos2){
foreach ($parts[2] as $pos3){
foreach ($parts[3] as $pos4){
foreach ($parts[4] as $pos5){
foreach ($parts[5] as $pos6 ){
echo implode('-', [$pos1,$pos2,$pos3,$pos4,$pos5,$pos6 ])."\n";
$total++;
}
}
}
}
}
}
echo $total;
}
go($parts);
答案 2 :(得分:0)
我想我明白你在这里想要什么......这是一种非常强力的处理方法,但应该有效:
<?php
$parts[0] = array('APXE');
$parts[1] = array('N','P',"S");
$parts[2] = array('300','400','600','700','800','900','1G4','1G8','2G0','2G5');
$parts[3] = array(
array('dependIndex'=>'2','dependVal'=>'300','values'=>array('025','050','075','150','200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'400','values'=>array('025','050','075','150','200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'600','values'=>array('1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'700','values'=>array('200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'800','values'=>array('075','150','200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'900','values'=>array('025','050','075','150','200','250','500','1M0','1M7','3M5')),
array('dependIndex'=>'2','dependVal'=>'1G4','values'=>array('075','150','250','500','1M0','1M7','3M5','7M0')),
array('dependIndex'=>'2','dependVal'=>'1G8','values'=>array('250','500','1M0','1M7','3M5','7M0','14M')),
array('dependIndex'=>'2','dependVal'=>'2G0','values'=>array('250','500','1M0','1M7','3M5','7M0','14M')),
array('dependIndex'=>'2','dependVal'=>'2G5','values'=>array('250','500','1M0','1M7','3M5','7M0','14M'))
);
$parts[4] = array(
array('dependIndex'=>'2','dependVal'=>'300','values'=>array('A0','A1','A2')),
array('dependIndex'=>'2','dependVal'=>'400','values'=>array('B0','B1','B2','C0')),
array('dependIndex'=>'2','dependVal'=>'600','values'=>array('D0')),
array('dependIndex'=>'2','dependVal'=>'700','values'=>array('E0')),
array('dependIndex'=>'2','dependVal'=>'800','values'=>array('F0')),
array('dependIndex'=>'2','dependVal'=>'900','values'=>array('G0','G2','G3','G4')),
array('dependIndex'=>'2','dependVal'=>'1G4','values'=>array('H0','H1')),
array('dependIndex'=>'2','dependVal'=>'1G8','values'=>array('K0')),
array('dependIndex'=>'2','dependVal'=>'2G0','values'=>array('I0')),
array('dependIndex'=>'2','dependVal'=>'2G5','values'=>array('J0','J1'))
);
$parts[5] = array('12','1L','24','48','AC');
$parts[6] = array('ET','FC','IC','FI','NT');
$parts[7] = array('OO');
foreach($parts[1] as $eigth){
foreach($parts[2] as $sect){
foreach($parts[3] as $keya => $firstloop){
foreach($parts[3][$keya] as $itemone){
foreach($parts[3][$keya]['values'] as $firstselector){
foreach($parts[4] as $keyb => $secondloop){
foreach($parts[4][$keyb] as $itemtwo){
foreach($parts[4][$keyb]['values'] as $secondselector){
foreach($parts[5] as $halve){
foreach($parts[6] as $quarter){
if($sect == $parts[3][$keya]['dependVal'] && $sect == $parts[3][$keyb]['dependVal']){
echo $parts[0][0] . "-" . $eigth . "-" . $sect . "-" . $firstselector . "-" . $secondselector . "-" . $halve . "-" . $quarter . "<br>";
}
}
}
}
}
}
}
}
}
}
}
?>
答案 3 :(得分:0)
我看到你在Facebook上寻求帮助,指向这篇帖子,并决定对此进行打击。
我改变了列出依赖关系的方式,但我相信这一点可行(尽管它有点匆匆和蛮力)。希望它能为你提供一个好的起点。
/*
* The following arrays should be formatted thusly:
* $exclusionlist[indexOfDeterminingColumn][indexOfDependentColumn] = array(
* 'ValueOfDeterminingColumn' => array('list', 'of', 'valid', 'values')
* );
*/
$dependencyList[3][2] = array(
'300' => array('025','050','075','150','200','250','500','1M0','1M7','3M5'),
'400' => array('025','050','075','150','200','250','500','1M0','1M7','3M5'),
'600' => array('1M7','3M5'),
'700' => array('200','250','500','1M0','1M7','3M5'),
'800' => array('075','150','200','250','500','1M0','1M7','3M5'),
'900' => array('025','050','075','150','200','250','500','1M0','1M7','3M5'),
'1G4' => array('075','150','250','500','1M0','1M7','3M5','7M0'),
'1G8' => array('250','500','1M0','1M7','3M5','7M0','14M'),
'2G0' => array('250','500','1M0','1M7','3M5','7M0','14M'),
'2G5' => array('250','500','1M0','1M7','3M5','7M0','14M')
);
$dependencyList[4][2] = array(
'300' => array('A0','A1','A2'),
'400' => array('B0','B1','B2','C0'),
'600' => array('D0'),
'700' => array('E0'),
'800' => array('F0'),
'900' => array('G0','G2','G3','G4'),
'1G4' => array('H0','H1'),
'1G8' => array('K0'),
'2G0' => array('I0'),
'2G5' => array('J0','J1')
);
$dependencyList[5][1] = array(
'N' => array('12'),
'P' => array('1L'),
'S' => array('24')
);
// $segments contains lists of all valid values for each segment of the part number
$segments[0] = array('APXE');
$segments[1] = array('N','P',"S");
$segments[2] = array('300','400','600','700','800','900','1G4','1G8','2G0','2G5');
$segments[3] = array('025','050','075','14M','150','1M0','1M7','200','250','3M5','500','7M0');
$segments[4] = array('A0','A1','A2','B0','B1','B2','C0','D0','E0','F0','G0','G2','G3','G4','H0','H1','I0','J0','J1','K0');
$segments[5] = array('12','1L','24','48','AC');
$segments[6] = array('ET','FC','IC','FI','NT');
$segments[7] = array('OO');
$partNumbers = array();
// Loop through each segment's array
foreach($segments[0] as $segment0)
if(!testForExclusion(${'segment'.key($dependencyList[0])}, key($dependencyList[0]), $segment0, 0, $dependencyList))
foreach($segments[1] as $segment1)
if(!testForExclusion(${'segment'.key($dependencyList[1])}, key($dependencyList[1]), $segment1, 1, $dependencyList))
foreach($segments[2] as $segment2)
if(!testForExclusion(${'segment'.key($dependencyList[2])}, key($dependencyList[2]), $segment2, 2, $dependencyList))
foreach($segments[3] as $segment3)
if(!testForExclusion(${'segment'.key($dependencyList[3])}, key($dependencyList[3]), $segment3, 3, $dependencyList))
foreach($segments[4] as $segment4)
if(!testForExclusion(${'segment'.key($dependencyList[4])}, key($dependencyList[4]), $segment4, 4, $dependencyList))
foreach($segments[5] as $segment5)
if(!testForExclusion(${'segment'.key($dependencyList[5])}, key($dependencyList[5]), $segment5, 5, $dependencyList))
foreach($segments[6] as $segment6)
if(!testForExclusion(${'segment'.key($dependencyList[6])}, key($dependencyList[6]), $segment6, 6, $dependencyList))
foreach($segments[7] as $segment7)
if(!testForExclusion(${'segment'.key($dependencyList[7])}, key($dependencyList[7]), $segment7, 7, $dependencyList))
$partNumbers[] = $segment0.'-'.$segment1.'-'.$segment2.'-'.$segment3.'-'.$segment4.'-'.$segment5.'-'.$segment6.'-'.$segment7;
echo count( $partNumbers );
echo '<p>';
echo implode('<br/>', $partNumbers);
function testForExclusion($determiningValue, $determiningSegment, $testValue, $testSegment, $exclusionList){
$excluded = false;
if(isset($exclusionList[$testSegment][$determiningSegment][$determiningValue])){
if(!in_array($testValue, $exclusionList[$testSegment][$determiningSegment][$determiningValue])){
$excluded = true;
}
}
return $excluded;
}
编辑:数据结构和依赖函数应该可以用于任何依赖项组合,因此这个解决方案应该是可扩展的。