PHP
CakePHP Tutorial for Beginners: What is, Why Use & Features
What is CakePHP? CakePHP is an open-source framework for the rapid development and maintenance of web...
A PHP array is a variable that stores more than one piece of related data in a single variable.
Think of an array as a box of chocolates with slots inside.
The box represents the array itself while the spaces containing chocolates represent the values stored in the arrays.
The diagram below illustrates the above syntax.
In this tutorial, you will learn-
Numeric arrays use number as access keys.
An access key is a reference to a memory slot in an array variable.
The access key is used whenever we want to read or assign a new value an array element.
Below is the syntax for creating numeric array in php.
<?php $variable_name[n] = value; ?>
Or
<?php $variable_name = array(n => value, …); ?>
HERE,
Let’s now look at an example of a numeric array.
Suppose we have 5 movies that we want to store in array variables.
We can use the example shown below to do that.
<?php $movie[0] = 'Shaolin Monk'; $movie[1] = 'Drunken Master'; $movie[2] = 'American Ninja'; $movie[3] = 'Once upon a time in China'; $movie[4] = 'Replacement Killers'; ?>
Here,
Each movie is given an index number that is used to retrieve or modify its value. Observe the following code-
<?php $movie[0]="Shaolin Monk"; $movie[1]="Drunken Master"; $movie[2]="American Ninja"; $movie[3]="Once upon a time in China"; $movie[4]="Replacement Killers"; echo $movie[3]; $movie[3] = " Eastern Condors"; echo $movie[3]; ?>
Output:
Once upon a time in China Eastern Condors
As you can see from the above examples, working with arrays in PHP when dealing with multiple values of the same nature is very easy and flexible.
Alternatively, the above array variables can also be created using the following code.
<?php $movie = array(0 => "Shaolin Monk", 1 => "Drunken Master", 2 => "American Ninja", 3 => "Once upon a time in China", 4 =>"Replacement Killers" ); echo $movie[4]; ?>
Output:
Replacement Killers
Below is the syntax for creating associative array in php.
<?php $variable_name['key_name'] = value; $variable_name = array('keyname' => value); ?>
HERE,
Let’s suppose that we have a group of persons, and we want to assign the gender of each person against their names.
We can use an associative array to do that.The code below helps us to do that.
<?php $persons = array("Mary" => "Female", "John" => "Male", "Mirriam" => "Female"); print_r($persons); echo ""; echo "Mary is a " . $persons["Mary"]; ?>
HERE,
Output:
Array ( [Mary] => Female [John] => Male [Mirriam] => Female ) Mary is a Female
Associative array are also very useful when retrieving data from the database.
The field names are used as id keys.
The advantage of multidimensional arrays is that they allow us to group related data together.
Let’s now look at a practical example that implements a php multidimensional array.
The table below shows a list of movies by category.
Movie title | Category |
---|---|
Pink Panther | Comedy |
John English | Comedy |
Die Hard | Action |
Expendables | Action |
The Lord of the rings | Epic |
Romeo and Juliet | Romance |
See no evil hear no evil | Comedy |
The above information can be represented as a multidimensional array. The code below shows the implementation.
<?php $movies =array( "comedy" => array("Pink Panther", "John English", "See no evil hear no evil"), "action" => array("Die Hard", "Expendables"), "epic" => array("The Lord of the rings"), "Romance" => array("Romeo and Juliet") ); print_r($movies); ?>
HERE,
Output:
Array ( [comedy] => Array ( [0] => Pink Panther [1] => John English [2] => See no evil hear no evil ) [action] => Array ( [0] => Die Hard [1] => Expendables ) [epic] => Array ( [0] => The Lord of the rings ) [Romance] => Array ( [0] => Romeo and Juliet ) )
Another way to define the same array is as follows
<?php $film=array( "comedy" => array( 0 => "Pink Panther", 1 => "john English", 2 => "See no evil hear no evil" ), "action" => array ( 0 => "Die Hard", 1 => "Expendables" ), "epic" => array ( 0 => "The Lord of the rings" ), "Romance" => array ( 0 => "Romeo and Juliet" ) ); echo $film["comedy"][0]; ?>
Output:
Pink Panther
Note: the movies numeric array has been nested inside the categories associative array
Operator | Name | Description | How to do it | Output |
---|---|---|---|---|
x + y | Union | Combines elements from both arrays | <?php $x = array('id' => 1); $y = array('value' => 10); $z = $x + $y; ?> | Array([id] => 1 [value] => 10) |
X == y | Equal | Compares two arrays if they are equal and returns true if yes. | <?php $x = array("id" => 1); $y = array("id" => "1"); if($x == $y) { echo "true"; } else { echo "false"; } ?> | True or 1 |
X === y | Identical | Compares both the values and data types | <?php $x = array("id" => 1); $y = array("id" => "1"); if($x === $y) { echo "true"; } else { echo "false"; } ?> | False or 0 |
X != y, x <> y | Not equal | <?php $x = array("id" => 1); $y = array("id" => "1"); if($x != $y) { echo "true"; } else { echo "false"; } ?> | False or 0 | |
X !== y | Non identical | <?php $x = array("id" => 1); $y = array("id" => "1"); if($x !== $y) { echo "true"; } else { echo "false"; } ?> | True or 1 |
The count function is used to count the number of elements that an php array contains. The code below shows the implementation.
<?php $lecturers = array("Mr. Jones", "Mr. Banda", "Mrs. Smith"); echo count($lecturers); ?>
Output:
3
The is_array function is used to determine if a variable is an array or not. Let’s now look at an example that implements the is_array functions.
<?php $lecturers = array("Mr. Jones", "Mr. Banda", "Mrs. Smith"); echo is_array($lecturers); ?>
Output:
1
This function is used to sort arrays by the values.
If the values are alphanumeric, it sorts them in alphabetical order.
If the values are numeric, it sorts them in ascending order.
It removes the existing access keys and add new numeric keys.
The output of this function is a numeric array
<?php $persons = array("Mary" => "Female", "John" => "Male", "Mirriam" => "Female"); sort($persons); print_r($persons); ?>
Output:
Array ( [0] => Female [1] => Female [2] => Male )
This function is used to sort the array using the key. The following example illustrates its usage.
<?php $persons = array("Mary" => "Female", "John" => "Male", "Mirriam" => "Female"); ksort($persons); print_r($persons); ?>
Output:
Array ( [John] => Male [Mary] => Female [Mirriam] => Female )
This function is used to sort the array using the values. The following example illustrates its usage.
<?php $persons = array("Mary" => "Female", "John" => "Male", "Mirriam" => "Female"); asort($persons); print_r($persons); ?>
Output:
Array ( [Mary] => Female [Mirriam] => Female [John] => Male )
What is CakePHP? CakePHP is an open-source framework for the rapid development and maintenance of web...
Training Summary PHP is the most popular scripting language on the web. Without PHP Facebook,...
In this tutorial, you will learn- PHP Data Types PHP Variable Use of variables Variable type...
What is a File? A file is simply a resource for storing information on a computer. Files are...
PHP is a server-side scripting language used to develop static and dynamic websites or web...
What is OOPs? Object Oriented is an approach to software development that models application...