0

I have a response like this:

  "data": [
            {

                "pay": "2010000",

            },
            {

                "pay": "3010000",

            },
            {

                "pay": "3920000",

            },
]

    foreach ($data as $data) {
              $sum += $data['pay'];

              array_push($newArr, array(
                        "finalResult" => '0',
                     )
                    );
            }

I want to get (individual pay divided by sum of all pay) results and should push this value into array. I have a foreach loop where I'm trying to get the final result. But the final result array is showing incorrect data. Is there anything I'm missing?

I tried this with two foreach loops like this. But I'm getting wrong result

foreach ($newArr as $newArr_key => $newArr_value) {

            if ($sum != 0) {
                $finalResult = $data['pay']/ $sum;


                if (isset($finalResult) && $finalResult != '') {
                    $newArr[$newArr_key]['finalResult'] = $finalResult;
                }

            } else {
                $finalResult = 'Division by Zero';
            }


            }
yer
  • 1,454
  • 2
  • 16
  • 33
  • 6
    This is not duplicate of the question you linked. – yer Apr 20 '18 at 21:02
  • 1
    what is the final result array? – drudge Apr 20 '18 at 21:02
  • "finalResult" : $finalResult – yer Apr 20 '18 at 21:02
  • 1
    The input data you show is neither a correct json nor an array also. Please correct your input data first and then ask question – Alive to die - Anant Apr 20 '18 at 21:05
  • You don't divide by sum of **all** pay. You divide be the sum of payments your loop processed until then. You can fix it with two foreach loops. – jh1711 Apr 20 '18 at 21:05
  • @yer if that is how your code is laid out, it seems that you're not closing the `data` JSON, nor are you closing the `array_push`. – drudge Apr 20 '18 at 21:05
  • 2
    Can you please correct the syntax errors in your question to create a [mcve]? I think I see the real problem, but it's difficult to address it with so many other problems in the way. – Don't Panic Apr 20 '18 at 21:05
  • @jh1711 I tried with two foreach loops. I edited the answer. Please check and see if I'm missing anything – yer Apr 20 '18 at 21:14
  • _"is showing incorrect data"_ -> after correcting the syntax I tested the 'function and it shows correct data. If you put `1,1,1` as your pay values it gives you `1, 0.5, 0,3333333` - which is 1/1, 1/2, 1/3. But the way you decode the json and what $data is initially (you have it twice!?) might be crucial – Jeff Apr 20 '18 at 21:24
  • @AlivetoDie I entered. Please Check – yer Apr 20 '18 at 21:31
  • for me it's working as expected: [gist](https://gist.github.com/jeffstagedoor/e9e11f6026c191a48dbbae699d7ccc7f) (I couldn't find a php fiddle that I could share..) – Jeff Apr 20 '18 at 21:31
  • @Jeff I'm getting the same values for example: finalResult: 0.5,0.5,0.5 – yer Apr 20 '18 at 21:33
  • found one: https://3v4l.org/iHINN – Jeff Apr 20 '18 at 21:35
  • then there must be different in you code that I don't see... – Jeff Apr 20 '18 at 21:38
  • 1
    What is the desired output please? – Syscall Apr 20 '18 at 21:39
  • An array with finalResult : individual pay / total pay – yer Apr 20 '18 at 21:40
  • Basically the percentage each pay is of the sum of pay – Echtniet Apr 20 '18 at 21:48

1 Answers1

2

You could use array_sum() with array_column() to compute the total sum of "pay". Then you could use array_map() to compute the average of individual sum along the total sum.

$data = [
    ["pay" => "2010000"],
    ["pay" => "3010000"],
    ["pay" => "3920000"],
];
$sum = array_sum(array_column($data, 'pay'));
$out = array_map(function($item) use($sum) {
    return ['finalResult' => $item['pay'] / $sum] ;
}, $data);
print_r($out);

Outputs:

Array
(
    [0] => Array
        (
            [finalResult] => 0.2248322147651
        )

    [1] => Array
        (
            [finalResult] => 0.33668903803132
        )

    [2] => Array
        (
            [finalResult] => 0.43847874720358
        )

)

Or :

$data = [
    ["pay" => "2010000"],
    ["pay" => "3010000"],
    ["pay" => "3920000"],
];
$sum = array_sum(array_column($data, 'pay'));
$out = array_map(function($item) use($sum) {
    return $item['pay'] / $sum ;
}, $data);
print_r($out);

Output:

Array
(
    [0] => 0.2248322147651
    [1] => 0.33668903803132
    [2] => 0.43847874720358
)
Syscall
  • 19,327
  • 10
  • 37
  • 52
  • 1
    Thank you @Jeff. Yer, don't forget to post an example of the desired output for your future questions to help answerers to understand what you really want. Thank you :) – Syscall Apr 20 '18 at 21:47