Php 仅从数组中选择特定值
我如何才能从该数组中仅获取具有cost\u measure==“Percent”的记录,然后仅迭代这些记录 这是我的阵列:Php 仅从数组中选择特定值,php,arrays,filter,Php,Arrays,Filter,我如何才能从该数组中仅获取具有cost\u measure==“Percent”的记录,然后仅迭代这些记录 这是我的阵列: array (size=5) 0 => array (size=8) 'cno' => string 'C-01' (length=4) 'cost_name' => string 'TroÅ¡ak prijevoz' (length=16) 'cost_description' => string
array (size=5)
0 =>
array (size=8)
'cno' => string 'C-01' (length=4)
'cost_name' => string 'Trošak prijevoz' (length=16)
'cost_description' => string 'Trošak prijevoza od Husvarne do K+N' (length=36)
'cost_type' => string 'Transport' (length=9)
'cost_measure' => string 'Percent' (length=7)
'cost_amount' => string '0.50' (length=4)
'cost_vcfc' => string 'Fixed cost' (length=10)
'custom_cost' => int 0
1 =>
array (size=8)
'cno' => string 'C-02' (length=4)
'cost_name' => string 'Carina' (length=6)
'cost_description' => string 'Carina na robu koja se uvozi van EU' (length=35)
'cost_type' => string 'Customs' (length=7)
'cost_measure' => string 'Percent' (length=7)
'cost_amount' => string '0.00' (length=4)
'cost_vcfc' => string 'Fixed cost' (length=10)
'custom_cost' => int 0
2 =>
array (size=8)
'cno' => string 'C-03' (length=4)
'cost_name' => string 'Banka' (length=5)
'cost_description' => string 'Troškovi banke za obradu transakcija' (length=37)
'cost_type' => string 'Bank' (length=4)
'cost_measure' => string 'Percent' (length=7)
'cost_amount' => string '0.15' (length=4)
'cost_vcfc' => string 'Fixed cost' (length=10)
'custom_cost' => int 0
3 =>
array (size=8)
'cno' => string 'C-04' (length=4)
'cost_name' => string 'PDV' (length=3)
'cost_description' => string 'Trošak poreza za državu' (length=25)
'cost_type' => string 'VAT' (length=3)
'cost_measure' => string 'Percent' (length=7)
'cost_amount' => string '25.00' (length=5)
'cost_vcfc' => string 'Fixed cost' (length=10)
'custom_cost' => int 0
4 =>
array (size=8)
'cno' => string 'C-06' (length=4)
'cost_name' => string 'Test cost' (length=9)
'cost_description' => string 'This one is to test the change' (length=30)
'cost_type' => string 'Main' (length=4)
'cost_measure' => string 'Absolute' (length=8)
'cost_amount' => string '120.00' (length=6)
'cost_vcfc' => string 'Fixed' (length=5)
'custom_cost' => int 1
在我获得cost_measure==“Percent”的记录之后,我需要将这些百分比添加到$number。例如:
$number + cost_amount 0.50 %
$number + cost_amount 0.15 %
$number + cost_amount 25 %
不确定需要什么(值或字符串)。请尝试以下操作:
$arr=array()//your array...
$number=0;$numberText="";
foreach($arr as $key=>$value){
if(strcmp($value["cost_measure"],"Percent") == 0){
$number = $number+$value["cost_amount"];
$numberText.="cost_amount ".$value["cost_amount"]." %<br>";
}
}
echo $number;//total percentage added.
echo $numberText;//string with all the percentages
$arr=array()//您的数组。。。
$number=0$numberText=“”;
foreach($arr作为$key=>$value){
如果(strcmp($value[“cost_measure”],“Percent”)==0){
$number=$number+$value[“成本金额”];
$numberText.=“成本金额”。$value[“成本金额”]。“%
”;
}
}
echo$number//增加的总百分比。
echo$numberText//用所有百分比串起来
是否要像示例中那样显示字符串?好,这工作不正常。它加起来是120%,从数组中加起来应该是25%,0.50%和0.15%,每个单独如下:$number*0.50%,$number*0.15%和$number*25%不求和。好的,我知道问题出在哪里了。它不仅得到了百分比,还得到了所有的美元价值[“成本衡量”]。这就是它变得太大的原因。它也变得绝对了。if(strcmp($value[“cost_measure”],“Percent”)=0)有点问题{好的,我知道了。重新修改了你的代码,这正是我想要的!非常感谢你的帮助!