我有一个看起来像这样的表格:
<form method="POST">
<p>Model 1</p>
<input name="data_quantity[]">
<input name="data_consignment[]">
<input name="data_price[]">
<p>Model 2</p>
<input name="data_quantity[]">
<input name="data_consignment[]">
<input name="data_price[]">
<p>Model 3</p>
<input name="data_quantity[]">
<input name="data_consignment[]">
<input name="data_price[]">
...
</form>
现在。当我在 PHP 上读取数据时,我在 POST 数组 ( $_POST
) 中有三个数组:$_POST['data_quantity'], $_POST['data_consignment'], $_POST['data_price']
...
我需要将它们合并在一起,所以我计算一个数组中的总数,然后我做了一个for
,然后我用帖子中每个数组中的值重新创建数组。快速示例:
public function build_Lineitems($Quantity=array(), $Consignment=array(), $TransferFrom=array(), $TransferTo=array(), $Price=array(), $Attributes=array()){
...
# On va boucler sur un des 6 tableaux...
$Lineitems = array();
foreach($Quantity as $ModelID => $Lineitem){
# Entrée dans le tableau
if(isset($Lineitems[$ModelID]) === FALSE){
$Lineitems[$ModelID] = array();
}
# Est-ce que c'est un tableau
if(is_array($Lineitem) === TRUE){
foreach($Lineitem as $Entry => $Clone){
$Lineitems[$ModelID][] = array(
'qty' => (isset($Quantity[$ModelID][$Entry]) === TRUE ? $Quantity[$ModelID][$Entry] : array()),
'con' => (isset($Consignment[$ModelID][$Entry]) === TRUE ? $Consignment[$ModelID][$Entry] : array()),
'xferfrom' => (isset($TransferFrom[$ModelID][$Entry]) === TRUE ? $TransferFrom[$ModelID][$Entry] : array()),
'xferto' => (isset($TransferTo[$ModelID][$Entry]) === TRUE ? $TransferTo[$ModelID][$Entry] : array()),
'price' => (isset($Price[$ModelID][$Entry]) === TRUE ? $Price[$ModelID][$Entry] : array()),
'attr' => (isset($Attributes[$ModelID][$Entry]) === TRUE ? (empty($Attributes[$ModelID][$Entry]) === FALSE ? explode(',', $Attributes[$ModelID][$Entry]) : array()) : array())
);
}
} else {
# Okay
$Lineitems[$ModelID] = array(
'qty' => (isset($Quantity[$ModelID]) === TRUE ? $Quantity[$ModelID] : array()),
'con' => (isset($Consignment[$ModelID]) === TRUE ? $Consignment[$ModelID] : array()),
'xferfrom' => (isset($TransferFrom[$ModelID]) === TRUE ? $TransferFrom[$ModelID] : array()),
'xferto' => (isset($TransferTo[$ModelID]) === TRUE ? $TransferTo[$ModelID] : array()),
'price' => (isset($Price[$ModelID]) === TRUE ? $Price[$ModelID] : array()),
'attr' => array()
);
}
}
# On renvoie
return $Lineitems;
}
我的问题,有没有更清洁的方法来做到这一点?谢谢。(this:多个数组的合并部分。