我有数组我想“优化”,而不是手动添加pg1-pg11,我想使用“for”。
如何使其成为核心?
$query = array(
'e1' => $_POST['npole3-1'],
'e2' => $_POST['npole3-2'],
'e3' => $_POST['npole3-3'],
'e4' => $_POST['npole3-4'],
'e5' => $_POST['npole3-5'],
'e6' => $_POST['npole3-6'],
'e7' => $_POST['npole3-7'],
'e8' => $_POST['npole3-8'],
'e9' => $_POST['npole3-9'],
'e10' => $_POST['npole3-10'],
'e11' => $_POST['npole3-11'],
'e12' => $_POST['npole3-12'],
'e13' => $_POST['npole3-13'],
'e14' => $_POST['npole3-14'],
'e15' => $_POST['npole3-15'],
'e16' => $_POST['npole3-16'],
'e17' => $_POST['npole3-17'],
'e18' => $_POST['npole3-18'],
);
for ($i = 1; $i <= 11; $i++) {array_push($query, ('pg1' => $_POST['pgadd-' . i]))};
答案 0 :(得分:0)
$query = array();
for ($i = 1; $i <= 11; $i++) {
// if exists in post array then
if(isset($_POST['pgadd-' . $i])){
// then add to query array
// array key will be like pg1, pg2, pg3 ...
// array value will be from your post data pgadd-1. pgadd-2
$query['pg'.$i] = $_POST['pgadd-' . $i];
}
}
// to see what's there inside array
print_r($query);