我用ajax Jquery做了一个帖子,工作得很好,但结果是这样的:
Array
(
[facdatas] => Array
(
[0] => Array
(
[mbr_id] => 26
)
[1] => Array
(
[nom] => Gautier Albert
)
[2] => Array
(
[adresse] => Avenue du Devin du Village 51
)
[3] => Array
(
[ville] => 1406 Cronay
)
[4] => Array
(
[concerne] => TEST
)
[5] => Array
(
[totalFac] => 118.00
)
)
[ligneFac] => Array
(
[0] => Array
(
[designation] => Veste de training CPNS
)
[1] => Array
(
[unite] => pièces
)
[2] => Array
(
[quantite] => 1
)
[3] => Array
(
[prixUnite] => 49
)
[4] => Array
(
[taxe] => 0.00
)
[5] => Array
(
[totLine] => 49.00
)
[6] => Array
(
[designation] => Pantalon de training CPNS
)
[7] => Array
(
[unite] => pièces
)
[8] => Array
(
[quantite] => 1
)
[9] => Array
(
[prixUnite] => 69
)
[10] => Array
(
[taxe] => 0.00
)
[11] => Array
(
[totLine] => 69.00
)
[12] => Array
(
[designation] =>
)
[13] => Array
(
[unite] =>
)
[14] => Array
(
[quantite] =>
)
[15] => Array
(
[prixUnite] =>
)
[16] => Array
(
[taxe] => 0.00
)
[17] => Array
(
[totLine] => 0.00
)
... ... ...
就像你从[12]行中看到的那样,行是空的或者是0.00,我尝试用array_filter($_POST[' linefac '])删除这些行。但结果是一样的。有一种方法来删除所有行的第一列是空的吗?
谢谢你的帮助
下面是向表单添加行代码:
$("#insLines").click(function()
{
$("#matable tbody").append('<tr>'+
'<td width="10px">'+
'<img id="insArticle" src="../../images/icon_add.png" width="16" height="16">'+
'</td>'+
'<td width="250px">'+
'<input name="ligneFac[][designation]" type="text" class="facBig" />'+
'</td>'+
' <td width="30">'+
'<input name="ligneFac[][unite]" type="text" class="facSmall" />'+
'</td>'+
' <td width="30">'+
'<input name="ligneFac[][quantite]" type="text" class="facSmall" />'+
'</td>'+
'<td width="30">'+
'<input name="ligneFac[][prixUnite]" type="text" class="facSmall" value="" />'+
'</td>'+
'<td width="30">'+
'<input name="ligneFac[][taxe]" type="text" class="facSmall" value="" />'+
'</td>'+
' <td width="30">'+
'<input name="ligneFac[][totLine]" type="text" class="facSmall" value="" />'+
'</td>'+
'</tr>');
iconeClicable();
});
请尝试这个演示代码:-
<?php
$arr=array();
$arr['ligneFac'] = array("designation"=>"", "unite"=>"pièces", "quantite"=>"","taxe"=>"0.00");
foreach($arr['ligneFac'] as $key =>$value)
{
if(!empty($value) && $value != "0.00"){
$arr2['ligneFac'][$key]=$value;
}
}
echo "<pre>";
print_r($arr2);
echo "</pre>";
?>
试试这个,这将删除没有值的条目
$data = array(array('key1' => 10),array('key2' => 20,),array('key3' => ''));
$result = array_filter(array_map('array_filter', $data));
从您发布的print_r()
的输出,我认为您的值都是字符串。在这种情况下,下面的代码就可以了:
$ligneFac = array_filter($_POST['ligneFac'], function($champs) {
$valeur = reset($champs);
return ( ('' !== $valeur) && ('0.00' !== $valeur) );
});
(要求PHP>= 5.3.0才能使用闭包)
foreach ($_POST['ligneFac'] as $i => $champs) {
list($cle, $valeur) = each($champs);
if ( ('taxe' !== $cle) && ( ('' === $valeur) || ('0.00' === $valeur) ) ) {
unset($_POST['ligneFac'][$i]);
}
}
感谢大家的帮助,这个问题现在解决了。
首先,我的表单的命名是错误的:
<input name="ligneFac[][designation]" type="text" class="facBig" />
我改变了:
<input name="ligneFac[1][designation]" type="text" class="facBig" />
这给了我一个数组linefacc line 1与指定值等…
接下来,我必须修改代码,添加一些新的行代码:
var nbr_tr = $('#fac_table tr').length;
var nbr_rows = nbr_tr -1; //to remove the header from calc.
newNum = nbr_rows + 1;
$("#matable tbody").append('<tr>'+
'<td width="10px">'+
'<img id="insArticle" src="../../images/icon_add.png" width="16" height="16">'+
'</td>'+
'<td width="250px">'+
'<input name="ligneFac['+newNum+'][designation]" type="text" class="facBig" />'+
'</td>'+
' <td width="30">'+
'<input name="ligneFac['+newNum+'][unite]" type="text" class="facSmall" />'+
'</td>'+
' <td width="30">'+
'<input name="ligneFac['+newNum+'][quantite]" type="text" class="facSmall" />'+
'</td>'+
'<td width="30">'+
'<input name="ligneFac['+newNum+'][prixUnite]" type="text" class="facSmall" value="" />'+
'</td>'+
'<td width="30">'+
'<input name="ligneFac['+newNum+'][taxe]" type="text" class="facSmall" value="" />'+
'</td>'+
' <td width="30">'+
'<input name="ligneFac['+newNum+'][totLine]" type="text" class="facSmall" value="" />'+
'</td>'+
'</tr>');
iconeClicable();
});
,现在结果数组是代码友好的…
Array
(
[mbrDatas] => Array
(
[1] => Array
(
[mbr_id] => 26
[nom] => Gautier Albert
[adresse] => Avenue du Devin du Village 51
[ville] => 1406 Cronay
)
)
[facdatas] => Array
(
[1] => Array
(
[concerne] => TEST
[totalFac] => 147.00
)
)
[ligneFac] => Array
(
[1] => Array
(
[designation] => Veste
[unite] => pièces
[quantite] => 1
[prixUnite] => 98
[taxe] => 0.00
[totLine] => 98.00
)
[2] => Array
(
[designation] => Veste de training
[unite] => pièces
[quantite] => 1
[prixUnite] => 49
[taxe] => 0.00
[totLine] => 49.00
)
)
)
,使用更新后的代码"Francis Eytan Dortort"。
foreach ($_POST['ligneFac'] as $i => $champs)
{
list($cle, $valeur) = each($champs);
if ( ('taxe' !== $cle) && ( ('' === $valeur) || ('0.00' === $valeur) ) )
{
unset($_POST['ligneFac'][$i]);
}
}