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';
            }


            }
17
  • 6
    This is not duplicate of the question you linked. Commented Apr 20, 2018 at 21:02
  • 1
    what is the final result array? Commented Apr 20, 2018 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 Commented Apr 20, 2018 at 21:05
  • 2
    Can you please correct the syntax errors in your question to create a minimal reproducible example? I think I see the real problem, but it's difficult to address it with so many other problems in the way. Commented Apr 20, 2018 at 21:05
  • 1
    What is the desired output please? Commented Apr 20, 2018 at 21:39

1 Answer 1

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
)
Sign up to request clarification or add additional context in comments.

1 Comment

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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.