Jump to content

Problems using in_array() for a processwire array fetched from database


desbest
 Share

Recommended Posts

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?

Link to comment
Share on other sites

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';

 

  • Like 5
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...