Array in PHP

PHP array is a special variable which can store multiple items in only one variable. Multiple items from an array are retrieved with array indexes. The array() built-in function is used to create an array in PHP programming. The basic structure of declaring an array is given below…

$a = array(index=>value);
PHP Array Discussion

PHP Array Discussion

There are three types of array in PHP. These are…

  1. Numeric array
  2. Associative array
  3. Multiple or Multidimensional array.

Numeric array in PHP

An array will be numeric array if the index is represented by numeric values. A numeric array can store numbers, strings or any object but their index will be numbers. A simple example is given below…

$a = array(0=>1,1=>”Sayeed”,2=>true);

Here, the variable $a is a numeric array because its indexes are declared by numbers.

If you want to start indexing from 0, you do not need to declare the index value because by default PHP uses indexing from 0. So, you can declare the above numeric array like below…

$a = array(1,”Sayeed”,true);

But if you want to start indexing with any numeric value except 0, you have to declare the numeric index explicitly. See below example…

$a = array(5=>1,”Sayeed”,true);

Here, array indexing starts from 5. So, the value of first index is 5 and the next indexes will be increased by one.

How to assign value in an array

There are different ways to assign values in a PHP numeric array. Several ways are given below…

Assigning value using array index sequentially

$a = array(7=>1,”Sayeed”,true);

Here, array indexing is starting from 7 and increasing sequentially by one.

Assigning value using random index value

$a = array(7=>”Apple”,10=>”Banana”, 15=>”Mango”);

Assigning value without index value

$a = array(1,”Sayeed”,true);

Here, array index is not being declared. So, PHP compiler will start indexing from 0 and increase values by one.

Declaring empty array first and then assigning value with sequential index

$a = array();

$a[] = 1;

$a[] = “Sayeed”;

$a[] = true;

Here, an empty array has been declared first and then items are being assigned without index. So, by default indexing will start from 0 and increase by one.

Declaring empty array first and then assigning value with random index value

$a = array();

$a[50] = 1;

$a[80] = “Sayeed”;

$a[100] =true;

First assigning value into array and then put value without index

$a = array(“Red”,”Blue”);

$a[] = “Green”;

Here, two items are being stored into an array first and then another item is being stored without index value. So, index value will be increased by one at this time.

How to print PHP array elements

There are various ways to print PHP array elements. Some easiest ways are shown below…

Print array elements with known index value

If the array index is known, the element can be printed directly with the index value. See below example…

<?php
 $a = array(1=>"Apple",10=>"Banana", 20=>;"Mango" );
 echo "The value of 10th index is ".$a[10];
 ?>

Output: The value of 10th index is Banana.

Print array elements using for loop statement

Knowing the array length, the array elements can be printed with for loop statement. See below example where for loop statement is being used to print array elements.


<?php

$a = array("Apple","Banana","Mango", "Orange");

$elements=count($a);echo "Array elements are: ";

for($i=0;$i<$elements;$i++){

echo $a[$i]." ";

}

?>

Output

Array elements are: Apple Banana Mango Orange

Here, the count() built-in function is being used to know the length of the array. Then for loop statement is being used to print array elements sequentially.

Print array elements using foreach loop statement

Using foreach loop statement, array elements can be printed easily without knowing the length of an array. See below example where foreach loop statement is retrieving items from an element…


<?php

$a = array("Apple","Banana","Mango", "Orange");

echo "Array elements are: ";

foreach($a as $value){

echo $value." ";

}

?>

Output

Array elements are: Apple Banana Mango Orange

PHP numeric array has been explained from very beginning in this article. If you face any problem to understand numeric array in PHP, feel free to contact with me from Contact us page. I’ll try my best to stay with you. PHP associative array, an important array in PHP, will be explained in next article. Spend some time to¬† study that article and improve your idea about PHP associative array.

No Responses

Leave a Comment

Your email address will not be published. Required fields are marked *