PHP Equivalent to the SQL IN (…) Statement

PHP, Pharmacy, XML Add comments

Recently I’ve been looking at using the NHS Business Service Authority’s Dictionary of Medicines and Devices for my own nefarious purposes. The Information (data that has meaning) is distributed as XML files.

I haven’t come across a database design for the data so I’ve come up with my own and to import the data into it I’ve been using PHP’s XML DOM functionality (a different technique than I described in my earlier post …). Because the files are published according to specified standards I decided to save myself some programming time by adopting their terminology for my database design so that I could write very little code to prepare the data and insert it into the database.

However, I wanted to test that the nodes I was expecting were what was being prepared for insertion into the database. One reason it may not is if the XML file has been reformatted with indents (mentioned in this post) What I needed was an equivalent to SQLs IN statement. The solution is to use PHP’s in_array() function like so…

$desirable_values = array(’CD’, ‘CDDT’, ‘CDPREV’, ‘DESC’);
if ( in_array( $variable_to_test, $desirable_values) ) {
//Do Your Stuff
}

It saves you having to test if your variable is equal to each of the variables you want to test thus making your code cleaner and shorter.

Leave a Reply

WP Theme & Icons by N.Design Studio | Akismet has gobbled 4,843 spam comments...Mmmm Tasty :-)
Entries RSS Comments RSS Log in