我有几个来自表单字段提交的数组,看起来像这样
[budget_type_id] => Array
(
[0] => 1
[1] => 2
[2] => 2
[3] => 5
)
[budget_f1] => Array
(
[0] => 100
[1] => 200
[2] => 300
[3] => 400
)
[budget_f2] => Array
(
[0] => 100
[1] => 200
[2] => 300
[3] => 400
)
我需要将这些数据合并为这样
[arr1] => Array
(
[budget_type_id] => 1
[budget_f1] => 100
[budget_f2] => 100
)
[arr2] => Array
(
[budget_type_id] => 2
[budget_f1] => 200
[budget_f2] => 200
)
[arr3] => Array
(
[budget_type_id] => 2
[budget_f1] => 300
[budget_f2] => 300
)
[arr4] => Array
(
[budget_type_id] => 5
[budget_f1] => 400
[budget_f2] => 400
)
合并此数据的最快捷/最有效的方法是什么?
更新 根据我的需要略微修改:
$contractBudgets = array();
for($i = 0 ; $i < count($formfields['budget_type_id']) ; $i++){
$newBudget = $this->modx->newObject('ContractBudget');
$newBudget->set('budget_type_id',$formfields['budget_type_id'][$i]);
$newBudget->set('budget_f1',$formfields['budget_f1'][$i]);
$newBudget->set('budget_f2',$formfields['budget_f2'][$i]);
$contractBudgets[] = $newBudget;
}
$newContract->addMany($contractBudgets);
答案 0 :(得分:3)
也许使用SPL MultipleIterator
$budget_type_id = array( 1, 2, 2, 5, );
$budget_f1 = array( 100, 200, 300, 400, );
$budget_f2 = array( 100, 200, 300, 400, );
$mi = new MultipleIterator(multipleIterator::MIT_KEYS_ASSOC);
$mi->attachIterator(new ArrayIterator($budget_type_id), 'budget_type_id');
$mi->attachIterator(new ArrayIterator($budget_f1), 'budget_f1');
$mi->attachIterator(new ArrayIterator($budget_f2), 'budget_f2');
$newArray = array();
foreach($mi as $details) {
$newArray[] = $details;
}
var_dump($newArray);
答案 1 :(得分:0)
$merged = array();
for($i = 0 ; $i < count($budget_type_id) ; $i++){
$merged[] = array(
'budget_type_id'=>$budget_type_id[$i],
'budget_f1' => $budget_f1[$i],
'budget_f2' => $budget_f2[$i]
);
}