Understanding Applications of Arrays in PHP

Arrays are simply collections of data organized into pairs, which makes the information easier to analyze or manipulate. With PHP, there are a wide variety of integrated functions which you can use to work with data, helping to simplify the procedure substantially.  Since PHP and mySQL are linked in most Apache frameworks, understanding how to use arrays effectively can ensure you get the most out of your dynamic websites.

Common applications of arrays include stored database entries with associated keys, such as statistics sites, game sites and social networks. Building out large databases of information requires organizing the data around keys so it can be stored and retrieved for future use.  Understanding how to work with arrays can make interfacing PHP with your SQL database system much easier since you can retrieve, store and work with extensive databases on the front and back-end of your site.

Single Dimensional Arrays

The most common types of arrays are associative arrays, which associate a given key with a value, while standard mathematical arrays are simply ordered columns of integers. For purposes of PHP, each key value in the array is required to be unique so you can easily identify each independent entry.
Suppose we have a sports team that is ranked second in its league – the elements of the array would be $team[‘name’], $rank[2]. In order to properly organize these teams into an array of rankings, we would need to define the N teams in order:

$league = array (“Team1”, “Team2”, “Team3”)

This essentially organizes the league as:

$league[0] = “Team1”;
$league[1] = “Team2”;
$league[2] = “Team3”;

Once you have stored the array you can recall the standings with a basic loop command that echoes the values: foreach ($league as $value) { echo $value; }. While this is an important way to store basic one dimensional data, many times it’s important to dive deeper by displaying a full set of multi-dimensional arrays.

Multi Dimensional Arrays

Often times, you can simplify your database queries by creating arrays which contain multiple dimensions of data.  Essentially you’re creating a vector of data so you can associate your key with multiple points of information.  This way you can store complete statistics on users, teams, profiles and entry values so you can utilize the dynamic statements within your PHP site.

Going back to our previous example, you might be able to note the team name, city and number of wins for a given team.

<?php
$league["team1"]["name"] = "Hawks";
$league["team1"]["city"] = "Atlanta";
$league["team1"]["wins"] = "60";
$league["team2"]["name"] = "Bears";
$league["team2"]["city"] = "Chicago";
$league["team2"]["wins"] = "40";
$league["team3"]["name"] = "Lions";
$league["team3"]["city"] = "Detroit";
$league["team3"]["wins"] = "30";
?>

This creates an array which features multiple dimensions of data for each team, effectively allowing you to analyze the data in a deeper sense.  You can then display this information with basic PHP array commands that allow you to showcase the depth of information in your database for users:

<?php
echo $teams["team 2"]["name"] .
 " is located in ".$teams["team 2"]["title"] .
 " and has ".$teams["team 2"]["salary"] .
 " wins.";
?>

0 responses so far ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment