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
PHP - JSON Question
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="Weasel" data-source="post: 467718" data-attributes="member: 9520"><p>Here's an example of how to use the array_filter function. As I don't see an array filter in your example, not sure what you did wrong. Next time, please include it in the thread, otherwise, I can't teach you. The below example has 2 ways of doing it, depending on your PHP version.</p><p></p><p>[CODE=php]<?php</p><p></p><p>$jsonstringPerson = '[ </p><p> {"FrstName":"Henry","Middlename":"","LastName":"Walton","Online":true,"DeptId":"4"},</p><p> {"FrstName":"Klaus","Middlename":"","LastName":"Mikaelson","Online":true,"DeptId":"2"},</p><p> {"FrstName":"Kylo","Middlename":"","LastName":"Ren","Online":false,"DeptId":"4"},</p><p> {"FrstName":"Stan","Middlename":"","LastName":"Lee","Online":false,"DeptId":"3"},</p><p> {"FrstName":"Kevin","Middlename":"","LastName":"McNally","Online":false,"DeptId":"3"},</p><p> {"FrstName":"Katherine","Middlename":"","LastName":"Pierce","Online":false,"DeptId":"2"},</p><p> {"FrstName":"Clint","Middlename":"","LastName":"Barton","Online":true,"DeptId":"3"},</p><p> {"FrstName":"Hope","Middlename":"van","LastName":"Dyne","Online":false,"DeptId":"3"}</p><p>]';</p><p></p><p>$persons = json_decode($jsonstringPerson, true);</p><p></p><p>// With the shorthand "Arrow" Function (fn) - from PHP7.4 and higher</p><p>$filtered = array_filter($persons, fn($person) => $person['DeptId'] === "3" || $person['Online'] === true);</p><p></p><p>// With an anonymous function (PHP7.3 and lower)</p><p>$filtered = array_filter($persons, function($person) {</p><p> return $person['DeptId'] === "3" || $person['Online'] === true;</p><p>});[/CODE]</p><p></p><p>Which, in both examples will output:</p><p></p><p>[CODE]array(6) {</p><p> [0]=></p><p> array(5) {</p><p> ["FrstName"]=></p><p> string(5) "Henry"</p><p> ["Middlename"]=></p><p> string(0) ""</p><p> ["LastName"]=></p><p> string(6) "Walton"</p><p> ["Online"]=></p><p> bool(true)</p><p> ["DeptId"]=></p><p> string(1) "4"</p><p> }</p><p> [1]=></p><p> array(5) {</p><p> ["FrstName"]=></p><p> string(5) "Klaus"</p><p> ["Middlename"]=></p><p> string(0) ""</p><p> ["LastName"]=></p><p> string(9) "Mikaelson"</p><p> ["Online"]=></p><p> bool(true)</p><p> ["DeptId"]=></p><p> string(1) "2"</p><p> }</p><p> [3]=></p><p> array(5) {</p><p> ["FrstName"]=></p><p> string(4) "Stan"</p><p> ["Middlename"]=></p><p> string(0) ""</p><p> ["LastName"]=></p><p> string(3) "Lee"</p><p> ["Online"]=></p><p> bool(false)</p><p> ["DeptId"]=></p><p> string(1) "3"</p><p> }</p><p> [4]=></p><p> array(5) {</p><p> ["FrstName"]=></p><p> string(5) "Kevin"</p><p> ["Middlename"]=></p><p> string(0) ""</p><p> ["LastName"]=></p><p> string(7) "McNally"</p><p> ["Online"]=></p><p> bool(false)</p><p> ["DeptId"]=></p><p> string(1) "3"</p><p> }</p><p> [6]=></p><p> array(5) {</p><p> ["FrstName"]=></p><p> string(5) "Clint"</p><p> ["Middlename"]=></p><p> string(0) ""</p><p> ["LastName"]=></p><p> string(6) "Barton"</p><p> ["Online"]=></p><p> bool(true)</p><p> ["DeptId"]=></p><p> string(1) "3"</p><p> }</p><p> [7]=></p><p> array(5) {</p><p> ["FrstName"]=></p><p> string(4) "Hope"</p><p> ["Middlename"]=></p><p> string(3) "van"</p><p> ["LastName"]=></p><p> string(4) "Dyne"</p><p> ["Online"]=></p><p> bool(false)</p><p> ["DeptId"]=></p><p> string(1) "3"</p><p> }</p><p>}</p><p>array(6) {</p><p> [0]=></p><p> array(5) {</p><p> ["FrstName"]=></p><p> string(5) "Henry"</p><p> ["Middlename"]=></p><p> string(0) ""</p><p> ["LastName"]=></p><p> string(6) "Walton"</p><p> ["Online"]=></p><p> bool(true)</p><p> ["DeptId"]=></p><p> string(1) "4"</p><p> }</p><p> [1]=></p><p> array(5) {</p><p> ["FrstName"]=></p><p> string(5) "Klaus"</p><p> ["Middlename"]=></p><p> string(0) ""</p><p> ["LastName"]=></p><p> string(9) "Mikaelson"</p><p> ["Online"]=></p><p> bool(true)</p><p> ["DeptId"]=></p><p> string(1) "2"</p><p> }</p><p> [3]=></p><p> array(5) {</p><p> ["FrstName"]=></p><p> string(4) "Stan"</p><p> ["Middlename"]=></p><p> string(0) ""</p><p> ["LastName"]=></p><p> string(3) "Lee"</p><p> ["Online"]=></p><p> bool(false)</p><p> ["DeptId"]=></p><p> string(1) "3"</p><p> }</p><p> [4]=></p><p> array(5) {</p><p> ["FrstName"]=></p><p> string(5) "Kevin"</p><p> ["Middlename"]=></p><p> string(0) ""</p><p> ["LastName"]=></p><p> string(7) "McNally"</p><p> ["Online"]=></p><p> bool(false)</p><p> ["DeptId"]=></p><p> string(1) "3"</p><p> }</p><p> [6]=></p><p> array(5) {</p><p> ["FrstName"]=></p><p> string(5) "Clint"</p><p> ["Middlename"]=></p><p> string(0) ""</p><p> ["LastName"]=></p><p> string(6) "Barton"</p><p> ["Online"]=></p><p> bool(true)</p><p> ["DeptId"]=></p><p> string(1) "3"</p><p> }</p><p> [7]=></p><p> array(5) {</p><p> ["FrstName"]=></p><p> string(4) "Hope"</p><p> ["Middlename"]=></p><p> string(3) "van"</p><p> ["LastName"]=></p><p> string(4) "Dyne"</p><p> ["Online"]=></p><p> bool(false)</p><p> ["DeptId"]=></p><p> string(1) "3"</p><p> }</p><p>}[/CODE]</p></blockquote><p></p>
[QUOTE="Weasel, post: 467718, member: 9520"] Here's an example of how to use the array_filter function. As I don't see an array filter in your example, not sure what you did wrong. Next time, please include it in the thread, otherwise, I can't teach you. The below example has 2 ways of doing it, depending on your PHP version. [CODE=php]<?php $jsonstringPerson = '[ {"FrstName":"Henry","Middlename":"","LastName":"Walton","Online":true,"DeptId":"4"}, {"FrstName":"Klaus","Middlename":"","LastName":"Mikaelson","Online":true,"DeptId":"2"}, {"FrstName":"Kylo","Middlename":"","LastName":"Ren","Online":false,"DeptId":"4"}, {"FrstName":"Stan","Middlename":"","LastName":"Lee","Online":false,"DeptId":"3"}, {"FrstName":"Kevin","Middlename":"","LastName":"McNally","Online":false,"DeptId":"3"}, {"FrstName":"Katherine","Middlename":"","LastName":"Pierce","Online":false,"DeptId":"2"}, {"FrstName":"Clint","Middlename":"","LastName":"Barton","Online":true,"DeptId":"3"}, {"FrstName":"Hope","Middlename":"van","LastName":"Dyne","Online":false,"DeptId":"3"} ]'; $persons = json_decode($jsonstringPerson, true); // With the shorthand "Arrow" Function (fn) - from PHP7.4 and higher $filtered = array_filter($persons, fn($person) => $person['DeptId'] === "3" || $person['Online'] === true); // With an anonymous function (PHP7.3 and lower) $filtered = array_filter($persons, function($person) { return $person['DeptId'] === "3" || $person['Online'] === true; });[/CODE] Which, in both examples will output: [CODE]array(6) { [0]=> array(5) { ["FrstName"]=> string(5) "Henry" ["Middlename"]=> string(0) "" ["LastName"]=> string(6) "Walton" ["Online"]=> bool(true) ["DeptId"]=> string(1) "4" } [1]=> array(5) { ["FrstName"]=> string(5) "Klaus" ["Middlename"]=> string(0) "" ["LastName"]=> string(9) "Mikaelson" ["Online"]=> bool(true) ["DeptId"]=> string(1) "2" } [3]=> array(5) { ["FrstName"]=> string(4) "Stan" ["Middlename"]=> string(0) "" ["LastName"]=> string(3) "Lee" ["Online"]=> bool(false) ["DeptId"]=> string(1) "3" } [4]=> array(5) { ["FrstName"]=> string(5) "Kevin" ["Middlename"]=> string(0) "" ["LastName"]=> string(7) "McNally" ["Online"]=> bool(false) ["DeptId"]=> string(1) "3" } [6]=> array(5) { ["FrstName"]=> string(5) "Clint" ["Middlename"]=> string(0) "" ["LastName"]=> string(6) "Barton" ["Online"]=> bool(true) ["DeptId"]=> string(1) "3" } [7]=> array(5) { ["FrstName"]=> string(4) "Hope" ["Middlename"]=> string(3) "van" ["LastName"]=> string(4) "Dyne" ["Online"]=> bool(false) ["DeptId"]=> string(1) "3" } } array(6) { [0]=> array(5) { ["FrstName"]=> string(5) "Henry" ["Middlename"]=> string(0) "" ["LastName"]=> string(6) "Walton" ["Online"]=> bool(true) ["DeptId"]=> string(1) "4" } [1]=> array(5) { ["FrstName"]=> string(5) "Klaus" ["Middlename"]=> string(0) "" ["LastName"]=> string(9) "Mikaelson" ["Online"]=> bool(true) ["DeptId"]=> string(1) "2" } [3]=> array(5) { ["FrstName"]=> string(4) "Stan" ["Middlename"]=> string(0) "" ["LastName"]=> string(3) "Lee" ["Online"]=> bool(false) ["DeptId"]=> string(1) "3" } [4]=> array(5) { ["FrstName"]=> string(5) "Kevin" ["Middlename"]=> string(0) "" ["LastName"]=> string(7) "McNally" ["Online"]=> bool(false) ["DeptId"]=> string(1) "3" } [6]=> array(5) { ["FrstName"]=> string(5) "Clint" ["Middlename"]=> string(0) "" ["LastName"]=> string(6) "Barton" ["Online"]=> bool(true) ["DeptId"]=> string(1) "3" } [7]=> array(5) { ["FrstName"]=> string(4) "Hope" ["Middlename"]=> string(3) "van" ["LastName"]=> string(4) "Dyne" ["Online"]=> bool(false) ["DeptId"]=> string(1) "3" } }[/CODE] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
PHP - JSON Question
Top