PHP Free Tutorial

Array in PHP

In PHP, Programming by Baqir Ali

A variable that stores one value is called a scalar variable, whereas an array is a variable that can store a collection of scalars, such as an array of numbers, an array of strings, an array of images, colors, and so on. If you want to define one number you would store it in a simple scalar variable, but if you want to store a whole list of numbers, it would be more practical to use an array and give the array a name to represent all of the numbers. Otherwise, you would have to create individual scalars for each number. It would be hard to come up with individual names for, let’s say, 100 numbers, or even for 10 numbers, but with an array, you can use one name to reference all of them. Based on how the individual elements are there are two types of arrays in PHP.

  • Indexed Array
  • Referenced Array

Indexed Array in PHP

Indexed arrays are similar to tables with a single column. An indexed array can contain zero or more elements. In PHP, like in many programming languages, the first element of an array is in the “zeroeth” position. An array with no elements has a zero-length.

Initializing an Array in PHP

Arrays are initialized with the array() function, which can take a list of comma-delimited values that become the elements in the new array. The following lines of code initialize a zero-length array and then adds four elements to the array.

Syntax
$topics = array();
$topics[0] = 'HTML';
$topics[1] = 'CSS';
$topics[2] = 'JavaScript';
$topics[3] = 'PHP';

The first line above is actually optional as the second line will create the array if one does not already exist. However, it is a better coding practice to explicitly initialize the array. The $topics array could also be created in a single line as follows.

$topics = array('HTML','CSS','JavaScript','PHP');

Example 1: Defining and initializing an array, print an element, then print the structure

<?php
$data = array();
$data[] = 10;
$data[] = 20;
$data[] = 40;
$data[] = 70;
$data[] = 90;
$data[] = "EVS";

echo("Data Output is ".$data[4]);

echo("<pre>");
print_r($data);
echo("</pre>");

?>

Output:

Data Output is 90
Array
(
    [0] => 10
    [1] => 20
    [2] => 40
    [3] => 70
    [4] => 90
    [5] => EVS
)

Loop through an array in PHP

The following code will loop through the entire $topics array outputting each element to the browser.

foreach ($topics as $topic)
{
      echo "$topic<br>";
}

Example 2: Another example of looping through an array

<?php
$items=array();
		$items[0]=710;
		$items[1]=420;
		$items[2]=30;
		$items[3]=340;
		$items[4]=420;
		$items[5]=402;
		$items[6]=404;
		$items[7]=430;
		$items[8]=40;

		for ($i=0; $i <count($items) ; $i++) { 
			echo $items[$i]." ";
		}

?>

Output:

710 420 30 340 420 402 404 430 40

Example 3: Loop through an array and find specific value then print its index.

<?php
$items=array();
		$items[0]=710;
		$items[1]=420;
		$items[2]=30;
		$items[3]=340;
		$items[4]=420;
		$items[5]=50;
		$items[6]=404;
		$items[7]=430;
		$items[8]=40;

		$find=40;

		for ($i=0; $i <count($items) ; $i++) { 
			if ($find==$items[$i]) {
				echo "Found at ".$i;
			}
		}
?>

Output:

Found at 8

Example 4: Loop through an array and find a specific value then print its index and number of times it appeared using foreach loop.

<?php
		$items=array();
		$items[0]=710;
		$items[1]=420;
		$items[2]=30;
		$items[3]=420;
		$items[4]=420;
		$items[5]=400;
		$items[6]=404;
		$items[7]=420;
		$items[8]=40;

		$find=420;
		$value=0;
		$count=0;
		foreach ($items as $abcd) {
			if($abcd==$find)
			{	
				$value=1;
				$count++;
			}
		}

		if ($value==1) {
			echo " \"".$find." \" Found \"".$count."\" Times";
		}
		else
			echo "Not Found";

?>

Output:

“420 ” Found “4” Times

Referenced or Associative Array

Whereas indexed arrays are indexed numerically, associative arrays are indexed using names. For example, instead of JavaScript being indexed as 3, it could be indexed as “clientside-script”.

Initializing Associative Arrays

Like with indexed arrays, we can initialize a zero-length associative array and then add elements.

$topics = array();
$topics ['tech1] = 'HTML';
$topics ['tech2'] = 'CSS';
$topics ['tech3'] = 'JavaScript';
$topics ['tech4'] = 'PHP';


OR

$topics = array('tech1' => 'HTML','tech2' => 'CSS','tech3' => 'JavaScript','tech4' => 'PHP');

Reading from Associative Arrays in PHP

Reading from associative arrays is as simple as reading from indexed arrays.

echo $topics['tech2']; //outputs CSS to the browser

Looping through Associative Arrays in PHP

The following code will loop through the entire $topics array outputting each element and its key to the browser.

Syntax
foreach ($topics $key => $topic)
{
echo "<b>$key:</b> $topic <br>";
}

foreach ($topics as $topic)
{
echo "$topic <br/>";
}

Example 5: Code Example Looping through Associative Array in PHP

<?php
		$temp=array('Sunday'=>45,'Monday'=>55,'Tuesday'=>55,'Wednesday'=>66,'Thursday'=>55,'Friday'=>50);

		foreach ($temp as $day) {
			echo $day." ";
		}
?>

Output:

45 55 55 66 55 50

Support us by sharing this post