An array is a variablethat stores more than onepiece of related data in a single variable. Think
of an array as a box of chocolateswith slotsinside. The box representsthe arrayitself while
the spacescontaining chocolates representthe valuesstored in the arrays.
Numeric Arrays
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 syntaxfor creating numeric arrays.
$variable_name[n] = value;
<?php
//create an associative array of persons
$persons = array('Mary' => 'Female', 'John' => 'Male', 'Mirriam' => 'Female');
print_r($persons); //print all contents of persons array
echo '<br>'; //create new line
echo 'Mary is a ' . $persons['Mary']; //get mary's gender
?>
Multi-dimensional arrays
These are arraysthat contain other nested arrays. The advantage of multidimensional arrays is
that they allow us to group related datatogether.
Count function
The count functionis used to countthe numberof elementsthat an array contains.
The code
below shows the implementation.
<?php
$lecturers = array('Mr. Jones', 'Mr. Banda', 'Mrs. Smith');
echo count($lecturers); //outputs 3
?>
is_array function
The is_array functionis used to determineif a variableis an arrayor 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);
?>
The above code outputs
1
Sort –
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);
?>
Arrays are special variableswith the capacityto store multi values.
Arrays are flexibilityand can be easily stretchedto accommodate more values.
Numeric arraysuse numbers for the array keys
Associative arraysuse descriptive namesfor array keys
The is_arrayfunction is used to determinewhether a variableis a valid array or not.
of an array as a box of chocolateswith slotsinside. The box representsthe arrayitself while
the spacescontaining chocolates representthe valuesstored in the arrays.
Numeric Arrays
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 syntaxfor creating numeric arrays.
$variable_name[n] = value;
<?php
//create an associative array of persons
$persons = array('Mary' => 'Female', 'John' => 'Male', 'Mirriam' => 'Female');
print_r($persons); //print all contents of persons array
echo '<br>'; //create new line
echo 'Mary is a ' . $persons['Mary']; //get mary's gender
?>
Multi-dimensional arrays
These are arraysthat contain other nested arrays. The advantage of multidimensional arrays is
that they allow us to group related datatogether.
Count function
The count functionis used to countthe numberof elementsthat an array contains.
The code
below shows the implementation.
<?php
$lecturers = array('Mr. Jones', 'Mr. Banda', 'Mrs. Smith');
echo count($lecturers); //outputs 3
?>
is_array function
The is_array functionis used to determineif a variableis an arrayor 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);
?>
The above code outputs
1
Sort –
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);
?>
Arrays are special variableswith the capacityto store multi values.
Arrays are flexibilityand can be easily stretchedto accommodate more values.
Numeric arraysuse numbers for the array keys
Associative arraysuse descriptive namesfor array keys
The is_arrayfunction is used to determinewhether a variableis a valid array or not.