What is an array?

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.

Related Posts:
  • Php-Configuration Control Through .htaccess The .htaccessfile is very powerful and can control more than just URL structure. For instance, you can control PHP configuration options using the .htaccessfile. To increase the memory allotted to PHP use this command: php_v… Read More
  • strtotime php-current time zone strtotime()parsing is always done with the current time zone, unless a different time zone is specified in the string that is parsed:<?phpecho date("H:i T\n", strtotime("09:22")); // shows 09:22 CETecho date("H:i T\n\n", … Read More
  • PHP INTERFACES Class inheritanceenables you to describe a parent-child relationshipbetween classes. For example, you might have a base class  Shapefrom which both Squareand Circlederive. However,  you might often want to add a… Read More
  • PHP Jobs interview-Common Section PHP Syntax Variables Operators Arrays If/Then Statements Switch Statements For Loops Foreach Loops While Loops Do While Loops User-Defined Functions Object Oriented Programming with PHP… Read More
  • PHP RSS feed script PHP RSS feed script RSS Reader PHP code function get_feed($link) {     $this->load->helper('text');     $last_update = time();     header('Cache-Control: no-cache, must-… Read More
  • PHP Session expire-minutes inactivity PHP Session expire-minutes inactivity session_cache_limiter('public'); session_cache_expire(15); //should expire after 15  minutes inactivity asy way to handle this, is to set a variable to $_SESSION  every time … Read More
  • Sorting Arrays-PHP PHP supports a variety of ways to sort an array when  I say sort, I am referring to an alphabetical sort if it is a string,  and a numerical sort if it is a number. When sorting an array,  you must k… Read More
  • Create Login page php-Php code  Create Login page php <?php             session_start();             $host="localhost"; // Host name &n… Read More
  • MySQL with php The basic steps of performing a query, whether using the mysql command-line tool or PHP, are the same:Connect to the database.Select the database to use.Build a SELECT statement.Perform the query.Display the results. Wh… Read More
  • What is array Arrays An arrayin PHP is a collection of key/value pairs.  This means that it maps keys or indexes to values. Array indexescan be either integers or strings whereas values can be of any type. Arrays in PHP are implement… Read More
  • parent AND self PHP oops  parent  AND self PHP oops self::refers to the current class and it is usually used to accessstatic members, methods, and constants. parent::refers to the  parent class and it is most often used when wanting … Read More
  • Building Dynamic Images-PHP You want to create an image based on a existing image template and dynamic data typically text). For instance, you want to create a hit counter. Load the template image, find the correct position to properly cente… Read More
  • Php Date or Time Simplest display of date or time is telling your users what time it is. Use the date( ) or strftime( ) strftime( ) says: Wed Oct 20 12:00:00 2004date( ) says: Wed, 20 Oct 2004 12:00:00 -0400 Both strftime( ) and date( )… Read More
  • Showing the Local Time in Other Time Zones Showing the Local Time in Other Time Zones Sometimes, you want  to show a formatted time in the current time zone and inother time zones as well. The following script shows a full textual date representation for the U.S… Read More
  • Static Methods-PHP  PHP supports declaring methods as static. Whatthis means is that your static methods are part of the  class and are not bound to any specific object instance and  its properties. Therefore, $this isn’t acce… Read More