WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

PHP 5 Arrays


An exhibit stores different qualities in a single variable:

Example

<?php
$color = array("RED", "BLUE", "GREEN ");
reverberation "I like " . $color[0] . ", " . $color[1] . " and " . $color[2] . ".";
?>
Run model »

What is an Array?

An exhibit is an exceptional variable, which can hold more than one incentive at a time.

If you have a rundown of things (a rundown of vehicle names, for instance), putting away the shading in single factors could look like this:

$color1 = "RED";
$color2 = "BLUE";
$color3 = "GREEN ";

However, imagine a scenario in which you need to circle through the shading and locate a particular one. Also, consider the possibility that you had not 3 shading, however 300?

.

The arrangement is to make an array!

An cluster can hold numerous qualities under a solitary name, and you can get to the qualities by alluding to a list number.


Create an Array in PHP

In PHP, the exhibit() work is utilized to make an array:

array();

In PHP, there are three kinds of arrays:

  • Indexed arrays - Arrays with a numeric index
  • Associative arrays - Arrays with named keys
  • Multidimensional arrays - Arrays containing at least one arrays

PHP Indexed Arrays

There are two different ways to make filed arrays:

The record can be appointed naturally (file continuously begins at 0), like this:

$color = array("RED", "BLUE", "GREEN ");

or the record can be doled out manually:

$color[0] = "RED";
$color[1] = "BLUE";
$color[2] = "GREEN ";

The following precedent makes a listed exhibit named $color, appoints three components to it, and afterward prints a content containing the cluster values:

Example

<?php
$color = array("RED", "BLUE", "GREEN ");
echo "I like " . $color[0] . ", " . $color[1] . " and " . $color[2] . ".";
?>
Run precedent »

Get The Length of an Array - The check() Function

The check() work is utilized to restore the length (the quantity of components) of an array:

Example

<?php
$color = array("RED", "BLUE", "GREEN ");
echo count($color);
?>
Run precedent »

Loop Through an Indexed Array

To circle through and print every one of the estimations of a recorded cluster, you could utilize a for circle, as this:

Example

<?php
$color = array("RED", "BLUE", "GREEN ");
$arrlength = count($color);

for($x = 0; $x < $arrlength; $x++) {
    reverberation $color[$x];
    reverberation "<br>";
}
?>
Run model »

PHP Associative Arrays

Associative exhibits are clusters that utilization named keys that you relegate to them.

There are two different ways to make an affiliated array: 

$age['James M. Cooley'] = "40";
$age['Georgia R. Bailey'] = "27";
$age['Mildred V. Parker'] = "55";

The named keys would then be able to be utilized in a script:

Example

<?php
$age = array("James M. Cooley"=>"40", "Georgia R. Bailey"=>"27", "Mildred V. Parker"=>"55");
reverberation "Peter is " . $age['James M. Cooley'] . " years old.";
?>
Run model »