Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
JSON Count keys
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Mythraus" data-source="post: 470900" data-attributes="member: 20012"><p>Provided I'm understanding this correctly you want to sum the A,B,C and D,E,F values.</p><p></p><p>The best method for this would be to reduce the current data into an associative array indexed by key and then sum manually from there. The snippet below implements the described behaviour.</p><p></p><p>[CODE=php]<?php</p><p></p><p>$json = '[{"Name":"A","Count":"3"},{"Name":"B","Count":"9"},{"Name":"C","Count":"1"},{"Name":"D","Count":"1"},{"Name":"E","Count":"4"},{"Name":"F","Count":"7"}]';</p><p></p><p>$data = json_decode($json);</p><p></p><p>// Reduce Method</p><p>$counts_reduce = array_reduce($data, function ($acc, $item) {</p><p> if (!isset($acc[$item->Name])) {</p><p> $acc[$item->Name] = 0;</p><p> }</p><p></p><p> $acc[$item->Name] += intval($item->Count);</p><p></p><p> return $acc;</p><p>}, array());</p><p></p><p></p><p>// Iterative Method</p><p>$counts_iter = array();</p><p></p><p>foreach($data as $item) {</p><p> if (!isset($counts_iter[$item->Name])) {</p><p> $counts_iter[$item->Name] = 0;</p><p> }</p><p></p><p> $counts_iter[$item->Name] += intval($item->Count);</p><p>}</p><p></p><p>// Can optionally use '??' with each item to ensure 0 is used as a default</p><p>$abc = $counts_reduce["A"] + $counts_reduce["B"] + $counts_reduce["C"];</p><p></p><p>$def = $counts_reduce["D"] + $counts_reduce["E"] + $counts_reduce["F"];</p><p></p><p>var_dump([</p><p> "counts_reduce" => $counts_reduce,</p><p> "counts_iter" => $counts_iter,</p><p> "abc" => $abc,</p><p> "def" => $def</p><p>]);[/CODE]</p></blockquote><p></p>
[QUOTE="Mythraus, post: 470900, member: 20012"] Provided I'm understanding this correctly you want to sum the A,B,C and D,E,F values. The best method for this would be to reduce the current data into an associative array indexed by key and then sum manually from there. The snippet below implements the described behaviour. [CODE=php]<?php $json = '[{"Name":"A","Count":"3"},{"Name":"B","Count":"9"},{"Name":"C","Count":"1"},{"Name":"D","Count":"1"},{"Name":"E","Count":"4"},{"Name":"F","Count":"7"}]'; $data = json_decode($json); // Reduce Method $counts_reduce = array_reduce($data, function ($acc, $item) { if (!isset($acc[$item->Name])) { $acc[$item->Name] = 0; } $acc[$item->Name] += intval($item->Count); return $acc; }, array()); // Iterative Method $counts_iter = array(); foreach($data as $item) { if (!isset($counts_iter[$item->Name])) { $counts_iter[$item->Name] = 0; } $counts_iter[$item->Name] += intval($item->Count); } // Can optionally use '??' with each item to ensure 0 is used as a default $abc = $counts_reduce["A"] + $counts_reduce["B"] + $counts_reduce["C"]; $def = $counts_reduce["D"] + $counts_reduce["E"] + $counts_reduce["F"]; var_dump([ "counts_reduce" => $counts_reduce, "counts_iter" => $counts_iter, "abc" => $abc, "def" => $def ]);[/CODE] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
JSON Count keys
Top