2017-02-02 6 views
1

이 배열에서 cost_measure == "Percent"의 레코드 만 가져 와서 해당 레코드 만 반복 할 수 있습니까? 이 배열배열의 특정 값만 선택하십시오.

내가 가진 : 나는 cost_measure 레코드를 얻을

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 

후 == "퍼센트"나는 $ 번호로이 비율을 추가해야합니다. 예를 들면 :

$number + cost_amount 0.50 % 
$number + cost_amount 0.15 % 
$number + cost_amount 25 % 
+0

당신이 예에서와 같은 문자열을 표시 싶어? –

답변

1

회원님이 (값 또는 문자열)이 .try를 원하는 것을 확인하십시오

$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 
+0

확인 이것은 올바르게 작동하지 않습니다. 그것은 120 %를 합산하고 배열에서 각각 25 %, 0.50 % 및 0.15 %를 더해야합니다 : $ number * 0.50 %, $ number * 0.15 % 및 $ number * 25 % 합산하지 마십시오. – Budimir

+0

그래, 어디서 문제인지 알 겠어. Percent는 모든 $ value [ "cost_measure"]를 얻지 못합니다. 그것이 너무 커지는 이유입니다. 절대성도 얻고 있습니다. 뭔가 잘못 됐어. (strcmp ($ value [ "cost_measure"], "Percent") == 0) { – Budimir

+0

괜찮아. 리바운드 당신은 약간의 코드이고 내가 원했던 것입니다! 도움을 주셔서 대단히 감사합니다! – Budimir

관련 문제