PHP Array with Numeric Indexed Values

PHP Array

PHP Array is used to store more than one element which is identified by the index. In PHP Arrays are of three types –

  • Index arrays
  • Associative arrays
  • Multidimensional arrays

Caution:To create an array you have to use array() function.

PHP Index Array

In PHP Index Array the data will be stored and the array will call with Numeric index. Or the index is a number.

There are two ways to create an indexed array

Syntax of Index Array

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		$arrayName = array("value of array");
		
		$arraName[0] = "value1"; 
		$arraName[1] = "value2";
		$arraName[2] = "value3";
		$arraName[3] = "value4";
	?>
  </body>
</html>

The above syntax is used to create an index array. The index array is created with 2 ways and both ways are shown in the above syntax example.

Example 1 – Index Array Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		$mobile = array("Samsung","Redmi","Honor","Apple");
		
		$mobile[0] = "Samsung";
		$mobile[1] = "Redmi";
		$mobile[2] = "Honor";
		$mobile[3] = "Apple";
		
	?>
  </body>
</html>

In the above example, we declare two arrays in different ways.

Example 2 – Index Array Example stores company names.

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		$mobile = array("Samsung","Redmi","Honor","Apple");
		echo "The best company of mobile are <br />" . $mobile[0] . "<br />" . $mobile[1] . "<br />" . $mobile[2] . "<br />" . $mobile[3];
	?>
  </body>
</html>

In the above example, we declare a array $mobile and print the value of this array.

PHP Array Example with Numeric index
Fig.1 – Array Example with Numeric index

To get the number of element in array

The count() function is used to get the number of element in an array.

Example 3 – To get the number of element in Array Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		$vehicle = array("car","bike","bicycle","truck");
		echo "The number of elements in array are " . count($vehicle);
	?>
  </body>
</html>

In the above example, we declare a array $mobile and print the value of this array.

PHP Indexed Array Example Code Syntax
Fig.2 – Indexed Array with vehicle code.

Loop in array

If you want to process group of variables in the for loop than the array is very helpful like the addition of 10 values and if these 10 values are kept in an array then you can calculate the addition in the for loop.

Example 3 – Loop in array Example

<!DOCTYPE html>
<html>
  <head>
   
  </head>
  
  <body>
	<?php
		$x = array(10,32,43,52);
		$arraylength = count($x);
		$sum = 0;
		
		for($i = 0; $i < $arraylength; $i++) 
		{
			$sum = $sum + $x[$i];
		}
		echo "The sum of the given array is " . $sum;
	?>
  </body>
</html>
PHP Array With Loop Example code
Fig.3 – Array With for Loop Code.
You can leave a response, or trackback from your own site.
Leave a Reply to the article


Learn PHP

Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!          Protected by Copyscape