PHP Arrays: definition, looping and sorting simple arrays

In contrast to the scalar variables that specify only one value of the variable, the variable array can hold multiple values in an array. Arrays are very handy for storing the multiple values ??of the database query, or in the form of records list, where each field provides a different key and value. Let’s see how we can define some variables that we use in arrays PHP.

PHP Arrays definition looping sorting
If the developer does not specify a key for each value in the array, PHP automatically sets the key number, starting from 0.

This code defines an array of $ arrMonths [], where each month of the year is an element in the array.

<?php
$arrMonths[] = 'January';
$arrMonths[] = 'February';
$arrMonths[] = 'March';
$arrMonths[] = 'April';
?>

PHP interpreter automatically identifies each key in the array, and assign a number each array element, starting from 0.

<?php
$arrMonths[0] = 'January';
$arrMonths[1] = 'February';
$arrMonths[2] = 'March';
$arrMonths[3] = 'April';
?>

Array Functions

Another method for determining the array is to use the default array function. Each value in the below array will hold key order from 0 to array length.

<?php
$arrMonths= array('January', 'February', 'March', 'April');
?>

This function creates an ordered array on the same principle as the numbered elements in the above example.

associative arrays

In some cases, the developer is not necessary that each value associated with the number, but that it was tied to a more descriptive key. Each of these keys must be tied to the value therefrom, and the term “associative.”

Just as in the case of a numbered array, code writers can create an associative array with one element at a time:

<?php
$arrBooks['Comic Books'] = 'Superman';
$arrBooks['Science Fiction'] = 'Dune';
$arrBooks['Fantasy'] = 'The Hobbit';
$arrBooks['Horror'] = 'Carrie';
?>

Array function is also useful for creating associative arrays. The symbol => binds to the value of the keyword phrase.

<?php
$arrBooks = array(
'Comic' => 'Superman',
'Science Fiction' => 'Dune',
'Fantasy' => 'The Hobbit',
'Horror' => 'Carrie');
?>

This is an array?

If you are not sure whether a variable is an array structure, the is_array function can test the variable for the similarity with the array.

<?php
$baseballTeams = array('Cardinals', 'Tigers', 'Astros');
$footballTeams = 'Cardinals, Lions, Texans';

if (is_array($baseballTeams)) {
echo ("Baseball Team1<br/>");
}
else {
echo ("?? Baseball Team2<br/>");
}

if (is_array($footballTeams)) {
echo ("Football Team1<br/>");
}
else {
echo ("?? Football Team2<br/>");
}

?>

Since the variable $ baseballTeams is an array (array definition function), and the variable $ footballTeams is a list of words separated by commas, we get the following code:

Baseball Team1
?? Football Team2

Looping between arrays

foreach loop goes through each element of the array. Author can display, run calculations or perform any other operation for each element in the array, because it is tied to the cycle.

<?php
$arrBooks = array(
'Comic' => 'Superman',
'Science Fiction' => 'Dune',
'Fantasy' => 'The Hobbit',
'Horror' => 'Carrie');

foreach ($arrBooks as $key => $value) {
print "$value is an example of a $key book.<br/>";
}
?>

As a result, we get the following:

Superman is an example of a Comic book.
Dune is an example of a Science Fiction book.
The Hobbit is an example of a Fantasy book.
Carrie is an example of a Horror book.

Sorting arrays

The sort function allows us to sort arrays either by numbering (the values ??in the form of numbers), or in alphabetical order (for literal values). Sorting function passes through each value, and re-sets them new keys.

<?php
$baseballTeams = array('Cardinals', 'Tigers', 'Astros');
sort($baseballTeams);

foreach ($baseballTeams as $key => $value) {
echo $value. "<br/>";
}
?>

As a result, we have:

Astros
Cardinals
Tigers

You may also like...