Ray Patrick Soucy

Taking advantage of the Array in PHP

Many people using PHP come from an OOP background.  Unfortunately OOP in PHP was an afterthought, so doing things the OOP way in PHP is not always the best way.

Often overlooked, the Array data type in PHP is very powerful, efficient, and comes with an assortment of functions to make your life easier.

Probably one of the most useful aspects of using arrays in PHP is when combined with the foreach statement. This allows you to quickly loop though an array with very little code.  Looking at much of the PHP code I post, you'll notice a pattern; I almost always represent data in an associative array, and collections of data as an indexed array of associative arrays, or a 2-dimensional array.  There is a reason for this.

In web application development, it is very common to be working with tabular data (usually because it is coming from a database).  By storing this data in a pair of nested arrays you gain the benefits of arrays in PHP which have been optimized for performance and utility.  Arrays have sorting functions, merge functions, split functions, and intersect functions that make it very easy to efficiently manipulate them.

With the lack of OOP object iteration support and strong typing in PHP, it makes sense to prefer arrays for most situations where you need to store and manipulate data.  The array is really the killer data structure in PHP (as the Object is in Java).

This is why you'll see that Easy MySQL for PHP returns result sets as an array of associative arrays.

Using the count() function we can determine how many elements are in an array efficiently, and use it as a check before we process data.  Using an associative array for the inner array allows to reference data by a named key, rather than requiring the use of nested loops.

Consider the following example.

<?php
require 'EasyMySql.php';

$db = new EasyMySql('127.0.0.1''username''password''database');
$db->connect();
$people $db->query("SELECT name, age, gender, email FROM person");
$db->close();

if (
count($people) > 0) {
?>
<table>
<tr>
    <th>Name</th>
    <th>Age</th>
    <th>Gender</th>
    <th>Email</th>
</tr>
<?php
    
foreach ($people as $person) {
?>
<tr>
    <td><?php echo $person['name']; ?></td>
    <td><?php echo $person['age']; ?></td>
    <td><?php echo $person['gender']; ?></td>
    <td><?php echo $person['email']; ?></td>
</tr>
<?php
    
// foreach
?>
<tr>
    <th colspan="4">Total: <?php echo count($people); ?></th>
</tr>
</table>
<?php } else { ?>
<p>No data was returned for your request.</p>
<?php 
// if .. else
?>

In this example, we leverage the combination of the indexed array and associative array to simplify the code require to embed results in the application page.  We could also make use of the sort() function to sort the data, but an ORDER BY statement on the SQL query would be more appropriate.

Hopefully this example will get you thinking about how arrays can be leveraged in PHP to yield better performance, cleaner syntax, and less code.