desbest Posted October 17, 2017 Posted October 17, 2017 Here is my code. <?php $options = $fieldtypes->get('FieldtypeOptions')->getOptions('trader_type'); print_r($user->trader_type); echo "<hr>"; if (in_array("1", $user->trader_type)) { echo "found!"; } ?> Below is what is printed on the page from the print_r() Quote ProcessWire\SelectableOptionArray Object ( [count] => 2 [items] => Array ( [1] => 1 [2] => 2 ) ) However the in_array() feature does not appear to be working, when I can see from what is on the screen, that it should echo "found!" as the number 1 is in the array as a value. What is going on and how do I fix it?
abdus Posted October 17, 2017 Posted October 17, 2017 You need to get regular PHP arrays (from any WireArray descending object, i.e. all PW collections) using $arr->getArray() if (in_array("1", $user->trader_type->getArray())) { echo "found!"; } https://processwire.com/api/ref/wire-array/get-array/ 4
kongondo Posted October 17, 2017 Posted October 17, 2017 FYI, sometimes it is simpler to do an in-memory search of the WireArray. E.g. $oneItem = $myWireArray->get("id=20");// id is a property in the WireArray if($oneItem) echo 'we got the item folks!'; $multipleItems = $myWireArray->find("price>500");// price is a property in the WireArray if($multipleItems->count()) echo 'We got several items'; 5
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now