我有一个模板:在模板的基础上,我想要排列数组,这是模板的一部分,并插入db每个排列数据的元素 实际上,模板的值是动态变化的,元素的数量也是动态的
$temp = ['server', 'test[server]', 'extra'];
$temp1 = ['server', 'test[server]'];
$temp2 = ['server'];
test [server] - :test取决于服务器; 数据属于模板:
$server = ['server1','server2','server3'];
$test = ['server1'=>['test1', 'test2'],
'server2' => ['test4', 'test5'],
'server3' => ['test7']];
$extra = ['a','b','c'];
我想要像permutaion那样:
FOR template $ temp
['server'=>'server1', 'test' => 'test1','extra'=>'a']
['server'=>'server1', 'test' => 'test1','extra'=>'b']
['server'=>'server1', 'test' => 'test1','extra'=>'c']
['server'=>'server1', 'test' => 'test1','extra'=>'a']
['server'=>'server1', 'test' => 'test2','extra'=>'a']
['server'=>'server1', 'test' => 'test2','extra'=>'b']
['server'=>'server1', 'test' => 'test2','extra'=>'c']
['server'=>'server2', 'test' => 'test4','extra'=>'a']
['server'=>'server2', 'test' => 'test4','extra'=>'b']
['server'=>'server2', 'test' => 'test4','extra'=>'c']
['server'=>'server2', 'test' => 'test5','extra'=>'a']
['server'=>'server2', 'test' => 'test5','extra'=>'b']
['server'=>'server2', 'test' => 'test5','extra'=>'c']
['server'=>'server3', 'test' => 'test7','extra'=>'a']
['server'=>'server3', 'test' => 'test7','extra'=>'b']
['server'=>'server3', 'test' => 'test7','extra'=>'c']
FOR模板$ temp1:
['server'=>'server1', 'test' => 'test1']
['server'=>'server1', 'test' => 'test2']
['server'=>'server2', 'test' => 'test4']
['server'=>'server2', 'test' => 'test5']
['server'=>'server3', 'test' => 'test7']
FOR模板$ temp2:
['server'=>'server1']
['server'=>'server2']
['server'=>'server3']
在每个我要插入数据库的permutaion元素之后: 插入表(服务器,测试,额外)值(每个都不是1个元素 我有内存问题,因为它有大数组)
答案 0 :(得分:0)
所以你需要这样的东西?当然,在您的查询中更改表和字段名称。您也可以根据需要访问$newArray
数组。
$test = array('server1' => array('test1', 'test2'),
'server2' => array('test4', 'test5'),
'server3' => array('test7')
);
$extras = array('a', 'b', 'c');
$newArray = array();
foreach ($test as $server => $testArray) {
foreach ($testArray as $test) {
foreach ($extras as $extra) {
$sql = "INSERT INTO tbl (server, test, extra)"
. " VALUES ('".$server."', '".$test."', '".$extra."')";
echo $sql."<br />";
$db->query($sql);
$newArray[] = array("server" => $server, 'test' => $test, 'extra' => $extra);
}
}
}
输出是:
INSERT INTO tbl (server, test, extra) VALUES ('server1', 'test1', 'a')
INSERT INTO tbl (server, test, extra) VALUES ('server1', 'test1', 'b')
INSERT INTO tbl (server, test, extra) VALUES ('server1', 'test1', 'c')
INSERT INTO tbl (server, test, extra) VALUES ('server1', 'test2', 'a')
INSERT INTO tbl (server, test, extra) VALUES ('server1', 'test2', 'b')
INSERT INTO tbl (server, test, extra) VALUES ('server1', 'test2', 'c')
INSERT INTO tbl (server, test, extra) VALUES ('server2', 'test4', 'a')
INSERT INTO tbl (server, test, extra) VALUES ('server2', 'test4', 'b')
INSERT INTO tbl (server, test, extra) VALUES ('server2', 'test4', 'c')
INSERT INTO tbl (server, test, extra) VALUES ('server2', 'test5', 'a')
INSERT INTO tbl (server, test, extra) VALUES ('server2', 'test5', 'b')
INSERT INTO tbl (server, test, extra) VALUES ('server2', 'test5', 'c')
INSERT INTO tbl (server, test, extra) VALUES ('server3', 'test7', 'a')
INSERT INTO tbl (server, test, extra) VALUES ('server3', 'test7', 'b')
INSERT INTO tbl (server, test, extra) VALUES ('server3', 'test7', 'c')