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="Skythrust" data-source="post: 470899" data-attributes="member: 87030"><p>Hi guys,</p><p></p><p>I am looking for a solution to count keys in a foreach</p><p></p><p>like how many counts value A, B and C have and D, E and F.</p><p></p><p></p><p>[CODE=json]$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>[/CODE]</p><p></p><p>I am able to echo the name and count via a foreach, and I have tried to use and IF statement like the example below;</p><p></p><p>[CODE=php]<?php</p><p> </p><p>$data = (json_decode($response, true));</p><p></p><p>foreach($data['tags'] as $record) {</p><p> if($record['name'] == "A" || $record['name'] == "B" || $record['name'] == "C" ||)</p><p> {count($record['count'])}</p><p>// echo $record['name']. ' - '. $record['count']. '<br>';</p><p></p><p>}</p><p></p><p>?>[/CODE]</p><p></p><p>Any idea how I can count the values to get this work?</p></blockquote><p></p>
[QUOTE="Skythrust, post: 470899, member: 87030"] Hi guys, I am looking for a solution to count keys in a foreach like how many counts value A, B and C have and D, E and F. [CODE=json]$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"}]' [/CODE] I am able to echo the name and count via a foreach, and I have tried to use and IF statement like the example below; [CODE=php]<?php $data = (json_decode($response, true)); foreach($data['tags'] as $record) { if($record['name'] == "A" || $record['name'] == "B" || $record['name'] == "C" ||) {count($record['count'])} // echo $record['name']. ' - '. $record['count']. '<br>'; } ?>[/CODE] Any idea how I can count the values to get this work? [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
JSON Count keys
Top