【问题标题】:Select only specific values from array仅从数组中选择特定值
【发布时间】:2017-02-02 10:19:11
【问题描述】:

我怎样才能只从该数组中获取具有 cost_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 '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 %

【问题讨论】:

  • 你想像例子中那样显示字符串吗?

标签: php arrays filter


【解决方案1】:

不确定你想要什么(值或字符串)。试试这个:

 $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

【讨论】:

  • 好的,这不能正常工作。它加起来是 120%,它应该从数组中加起来 25%、0.50% 和 0.15%,每个分别像这样:$number*0.50%、$number*0.15% 和 $number*25% 不求和。
  • 好的,我知道问题出在哪里了。它不仅得到百分比,而且得到所有 $value["cost_measure"]。这就是它变得太大的原因。它也变得绝对。 if(strcmp($value["cost_measure"],"Percent") == 0){ 出了点问题
  • 好的,我知道了。撤销你的代码,这正是我想要的!非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2016-04-17
  • 1970-01-01
  • 2021-10-05
  • 1970-01-01
  • 1970-01-01
  • 2021-08-03
  • 2011-12-10
  • 1970-01-01
相关资源
最近更新 更多