Storing data in arrays

From Knowledge Kitchen
Jump to navigation Jump to search


Let's imagine that you had the following data set:

id title description image url
4242 e-Pipe This is not a pipe images/epipe.jpg
424 e-Cigar Just like the real cohiba images/ecigar.jpg
857 e-Cigarette Smells just like Winston Lights images/ecigarette.jpg
463456 e-Joint Legal in 14 out of 50 images/ejoint.jpg
3465 e-Cigarillo Small and thin images/ecigarillo.jpg
5 e-Hookah Social smoking in the digital era images/ehookah.jpg


Note that this data has rows and columns, so you could call this a two-dimensional data set, or a tabular data set.

An easy way to manage tabular data in PHP is to store it in one-dimensional or two-dimensional arrays.

One-dimensional arrays

Creating one-dimensional arrays

if you are not comfortable with multi-dimensional arrays, you could store this data in a set of several arrays like this:

 
 $ids = array("4242", "424","857", "463456", "3465", "5");

 $titles = array("e-Pipe", "e-Cigar", "e-Cigarette", "e-Joint", "e-Cigarillo", "e-Hookah");

 $descriptions = array("This is not a pipe", "Just like the real cohiba", "Smells just like Winston Lights", "Legal in 14 out of 50","Small and thin", "Social smoking in the digital era");

 $images = array("images/epipe.jpg", "images/ecigar.jpg", "images/ecigarette.jpg", "images/ejoint.jpg", "images/ecigarillo.jpg", "images/ehookah.jpg");

Looping through one-dimensional arrays

A simple way to loop through these arrays and output the products would be like this:

 
 		<?php for ($i=0; $i<sizeof($titles); $i++) : ?>
  
 <?php include("_product_array_template.php"); ?>
 
 		<?php endfor; ?>

This assumes you have an external file named _product_array_template.php that contains the code to output for each product.

External file templates

The code above would work well with an external file named _product_array_template.php that had the following code:

 
 		<div>
 			<h2><?php echo $titles[$i]; ?></h2>
 			<p><?php echo $descriptions[$i]; ?></p>
 			<img src="<?php echo $images[$i]; ?>" />
 		</div>


Multi-dimensional arrays

Creating multi-dimensional arrays

Better than using one-dimensional arrays, you could store the entire data set in a multi-dimensional array, specifically a two-dimensional array.

It turns out that two-dimensional arrays are perfectly suited to accommodate tabular data (data with rows and columns). Eventually we will want to use two-dimensional arrays to store most data like this.

 
 $products = array(
 	array(
 		"id" => "4242",
 		"title" => "e-Pipe",
 		"description" => "This is not a pipe",
 		"image" => "images/epipe.jpg"
 	),
 	array(
 		"id" => "424",
 		"title" => "e-Cigar",
 		"description" => "Just like the real cohiba",
 		"image" => "images/ecigar.jpg"
 	),
 	array(
 		"id" => "857",
 		"title" => "e-Cigarette",
 		"description" => "Smells just like Winston Lights",
 		"image" => "images/ecigarette.jpg"
 	),
 	array(
 		"id" => "463456",
 		"title" => "e-Joint",
 		"description" => "Legal in 14 out of 50",
 		"image" => "images/ejoint.jpg"
 	),
 	array(
 		"id" => "3465",
 		"title" => "e-Cigarillo",
 		"description" => "Small and thin",
 		"image" => "images/ecigarillo.jpg"
 	),
 	array(
 		"id" => "5",
 		"title" => "e-Hookah",
 		"description" => "Social smoking in the digital era",
 		"image" => "images/ehookah.jpg"
 	)
 );

Looping through multi-dimensional arrays

To loop through this type of array data set, you could use a foreach loop, like this. Each element in the $products array would be temporarily stored in a variable named $product.

 
 		<?php foreach ($products as $product) : ?>
  
 <?php include("_product_array_template.php"); ?>
 
 		<?php endfor; ?>

This assumes you have an external file named _product_array_template.php that contains the code to output for each product.


External file templates

The code above would work well with an external file named _product_array_template.php that had the following code:

 
 		<div>
 			<h2><?php echo $product["title"]; ?></h2>
 			<p><?php echo $product["description"]; ?></p>
 			<img src="<?php echo $product["image"]; ?>" />
 		</div>


What links here