0

I am working on a php script where I have a json api array that looks something like this --

[body] => stdClass Object
        (
            [api] => stdClass Object
                (
                    [status] => 200
                    [message] => GET stat....
                    [results] => 379
                    [filters] => Array
                        (
                            [0] => gameId
                            [1] => playerId
                        )

                    [statistics] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [gameId] => 17
                                    [totReb] => 7
                                    [assists] => 5
                                    [pFouls] => 4
                                    [steals] => 3
                                    [turnovers] => 2
                                    [blocks] => 0
                                    [plusMinus] => 9
                                    [playerId] => 265
                                )

                            [1] => stdClass Object
                                (
                                    [gameId] => 24
                                    [teamId] => 7
                                    [totReb] => 
                                    [assists] => 
                                    [pFouls] => 
                                    [steals] => 
                                    [turnovers] => 
                                    [blocks] => 
                                    [plusMinus] => 
                                    [playerId] => 265
                                )

And I am trying to get the values for each object to then output the total, so for each [?] => stdClass Object it will get the specified key and total the value.

How can I add all the values from one key?

3
  • What is your key? which values you want to add / count as total? can you try explain better what error do you get or what non desire output you get? Commented May 20, 2019 at 21:57
  • i will be using multiple keys in my script, so for example this key - [steals] => 3 Commented May 20, 2019 at 23:20
  • did my post helped you? Commented May 22, 2019 at 11:50

2 Answers 2

1

First get the array of statistics (the one you want to loop and sum on). Then use array_column to extract only specific key and then array_sum for summing it.

This is pseudo but should give you the idea:

$arr = $obj->body->api->statistics;
$keyToSum = "steals";
$totalSteals = array_sum(array_column($arr, $keyToSum));

Reference: array-column, array-sum

Sign up to request clarification or add additional context in comments.

Comments

0

There is one custom function I have which fetches column data recursively like array_column.

function array_column_recursive(array $haystack, $needle) {
    $found = [];
    array_walk_recursive($haystack, function($value, $key) use (&$found, $needle) {
        if ($key == $needle)
            $found[] = $value;
    });
    return $found;
}
echo array_sum(array_column_recursive($arr, 'steals'));

Source link.

Comments

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.