PHP: Function to display array contents with optional exit

Our hospital recently implemented the iPeople Echo Data Repository,  allowing us to pull data from our Meditech Hospital Information System,  into a SQL Server data base where we can access it like normal people,  instead of relying on Meditech’s internal report writing language (NPR) to spit out text based reports.

This has allowed me to do things that were previously next to impossible,  such as providing near real-time patient census data.

I will publish some entries on this,  even though they are industry specific,  but that’s not what I’m here to do today.

Because of how the data is stored,  and the complexity of some of the requests I get,  I usually end up storing information in associative arrays within my PHP script.

Many of these arrays are built programmatically.

As I test things,  I often need to check the structure and contents of these arrays.

Naturally,  I would plug in the following code:


<?php

echo '<pre>';  //this is html tag for 'pretty print'.  It displays output in a readable format

print_r($array);

echo '</pre>';

exit;  //  I usually threw in an exit as some of the scripts could take a while to run,  and they all spit out formatted XML

?>

On a recent high stress, short deadline project,  I was faced with data that made no sense at all.  I needed to associate data from several tables from Meditech’s payroll module,  but there wasn’t any clear association at the level I needed.

As I was constantly trying new queries in PHP,  I was tired of typing the code above,  so finally I created a function called “showarray()”.

Here’s the syntax:


showarray($array,[exit = true]);

So, instead of typing all that,  I type one line of code,  and if I don’t want it to exit when done,  I put false as a second parameter.

Here’s the function:


function showarray($out,$exit = true) {
echo '<pre>';

print_r($out);

echo '</pre>';

if ($exit == true) {

exit;

}

}

It really helped speed things along…

Advertisements