How count two arrays

raimis

Member
Have repeat group
count-currency.PNG
get
$CostRealSum1 = FabrikWorker::JSONtoData($data['bookin_18_repeat___real'], true);
$CostRealQnt1 = FabrikWorker::JSONtoData($data['bookin_18_repeat___quantity'], true);
$CostRealCurr1 = FabrikWorker::JSONtoData('{bookin_18_repeat___currency_raw}', true);

all these in array for me need count: (Quantity * Real) * Currency(here value is rate of exchange) = one row total finaly need get total with all rows of Real

how need write right foreach code for this counting ? :/
 
trying but not work correctly

$CostRealTotal1 = array();
foreach($CostRealSum1 as $index => $value) {
$CostRealTotal1[$index] = isset($CostRealQnt1[$index]) ? $CostRealQnt1[$index] * $value : $value;
foreach($CostRealCurr1 as $cur) {
if($cur === 1){
$CostRealTotal1[$index] = $CostRealTotal1[$index] + $cur;
}
}
}
$RealTotal1 = array_sum($CostRealTotal1);
 
First, it would be a lot easier if you pasted your code inside a code block so it formats better. Use the insert->code icons along the top.

I think your if statement is checking to see if the value of $cur is exactly an integer with a value of 1. Is that what you want? Often the results of the JSONtoData could be strings so this comparison will always fail.

Why bother with the total array, simply make $CostRealTotal1 a var and just add the value of $CostRealTotal1[$index] * $cur; Note also that I think you wanted a multiply value not an addition value of the $cur.

I also don't think you need all that complexity, how about this:

Code:
$CostRealSum1 = FabrikWorker::JSONtoData($data['bookin_18_repeat___real'], true);
$CostRealQnt1 = FabrikWorker::JSONtoData($data['bookin_18_repeat___quantity'], true);
$CostRealCurr1 = FabrikWorker::JSONtoData('{bookin_18_repeat___currency_raw}', true);

$CostRealTotal1 = 0;
foreach($CostRealSum1 as $index => $value) {
    $CostRealTotal1 += (isset($CostRealQnt1[$index]) ? $CostRealQnt1[$index] * $value : $value) * $CostRealTotal1[$index];
}
 
Thanks! How in this forech insert repeat checkbox(in all lines) is checked plus 21% with $CostRealTotal1
how.PNG
Code:
$Real = FabrikWorker::JSONtoData('{bookin_18_repeat___real}', true);
$VAT = FabrikWorker::JSONtoData('{bookin_18_repeat___vat}', true);
if ($VAT is checked ){
   $CostRealTotal1 + $VAT;
}
 
Last edited:
Not sure but the easiest way to figure it out is to put a print_r($VAT); exit; right before the if statement and see what you get. That should point you in the right direction.
 
get array. if check like in picture just one show this:
Code:
Array
(
    [0] => Array
        (
            [0] => 21
        )

)
if check all get this:
Code:
Array
(
    [0] => Array
        (
            [0] => 21
        )

    [1] => Array
        (
            [0] => 21
        )

    [2] => Array
        (
            [0] => 21
        )

    [3] => Array
        (
            [0] => 21
        )
)
 
It'll be keyed by the repeat count. So the second one would be [1].

Remember that HTML doesn't submit anything for checkboxes that aren't selected, so unlike most element types, there will be gaps in the request data array. But the array indexing will jive with the repeat count.

-- hugh
 
So in your for loop you would insert
Code:
if (array_key_exists($index, $VAT))
   CostRealTotal1 += $VAT($index);

or something similar.
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top