PHP Arrays

What is Array in PHP?

PHP Arrays. An array is a data structure that stores one or more similar type of values in a single value. For example, if you want to store 100 numbers then instead of defining 100 variables it’s easy to define an array of 100 lengths.

php arrays
How you can use arrays in PHP 7

Why do you need to use an array?

An array is a data structure, which can store a fixed-size collection of elements of the same data type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Types Of Arrays in PHP 7?

In PHP 7, there are three types of arrays:

  1. Indexed arrays – Arrays with a numeric index
  2. Associative arrays – Arrays with named keys
  3. Multidimensional arrays – Arrays containing one or more arrays

1. Numerically Indexed Arrays

Let’s assume that you’ve been tasked with creating a simple website for a local office supply company. And you’re currently working on the section devoted to paper. One way to manage the various items of stock in this category would be to place them in a numeric array. Basically, You can see the simplest way of doing. So, in the Example given below.

<?php
 $paper[] = "Copier";
 $paper[] = "Inkjet";
 $paper[] = "Laser";
 $paper[] = "Photo";
 print_r($paper);
?>
Index Array
Index Array

In this example, each time you assign a value to the array $paper, the first empty location within that array is used to store the value, and a pointer internal to PHP is incremented to point to the next free location, ready for future insertions. As your familiar print_r function is used to verify that the array has been correctly populated. Here, It prints out the following:

Array
(
[0] => Copier
[1] => Inkjet
[2] => Laser
[3] => Photo
)

The previous code could also have been written as shown in Example 6-2, where the exact location of each item within the array is specified. But, as you can see, that approach requires extra typing and makes your code harder to maintain. So, if you want to insert or remove supplies from the array. So, unless you wish to specify a different order, it’s usually better to simply let PHP handle the actual location numbers.

Example 6-2. Adding items to an array using explicit locations

<?php
$paper[0] = "Copier";
$paper[1] = "Inkjet";
$paper[2] = "Laser";
$paper[3] = "Photo";
print_r($paper);
?>

The output from these examples is identical, but you are not likely to use print_r in a developed website. So, Example 6-3 shows how you might print out the various types of paper the website offers using a for a loop.

Example 6-3. Adding items to an array and retrieving them

<?php
 $paper[] = "Copier";
 $paper[] = "Inkjet";
 $paper[] = "Laser";
 $paper[] = "Photo";
 for ($j = 0 ; $j < 4 ; ++$j)
 echo "$j: $paper[$j]<br>";
?>

This example prints out the following:

0: Copier
1: Inkjet
2: Laser
3: Photo

So far, you’ve seen a couple of ways in which you can add items to an array and one way
of referencing them, but PHP offers many more—which I’ll get to shortly. But first, we’ll
look at another type of array

Get The Length of an Array – The count() Function

The functioncount() is used to return the length or the number of elements of an array:

 
<?php
$cars = array("Volvo", "BMW", "Toyota");
 echo count($cars); 
?> 

Associative arrays in PHP 7

Keeping track of array elements by index works just fine, but can require extra work in terms of remembering which number refers to which product. It can also make code hard for other programmers to follow.
This is where associative arrays come into their own. Using them, you can reference the items in an array by name rather than by number.

You can read about associative arrays on the next page.

<< Prev Next >>

Loading