PHP  Arrays

PHP Arrays are used to store several values in a single variable.

Example of a PHP Arrays

<?php

$animals= array("Cow", "Goat", "Sheep");

echo "Array Values " . $animals[0] . ", " . $animals[1] . " and " . $animals[2] . ".";

?>

Array is a variable that has the capability of holding more than one value at the same time.

Above example is a array that holds different animals.If we did not use array we could have several variables defined to store values i.e

$animal1 = “Cow”;
$animal2 = “Goat”;
$animal3 = “Sheep”;

Array values can be accessed using the index number.

How to Create  PHP Arrays

PHP arrays are created using the array keyword and parenthesis enclose the array values.

Types of PHP Arrays

Three types of PHP Arrays.Namely

1.Indexed Arrays-These are arrays with numeric index

2.Associative Arrays-Arrays that have named key

3. Multidimensional arrays–are special types of arrays that contain 1 or more arrays.

PHP Indexed Arrays

Indexed arrays can be created in two ways.

1.Automatically assigning  Array Index

$animals = array(“Cow”, “Goat”, “Sheep”);

2.Manually assigning Array Index

$animals[0] = “Cow”;
$animals[1] = “Goat”;
$animals[2] = “Sheep”;

Below example creates array animals and prints the value in the array.
<?php

$animals= array("Cow", "Goat", "Sheep");

echo "Array Values " . $animals[0] . ", " . $animals[1] . " and " . $animals[2] . ".";

?>

See Also:PHP Functions

How to Get PHP Arrays Length Using Function Count

PHP built  count() function  is used to get the length of array.Once the function is called and given the array as argument,it returns the array length.

<?php

$animals= array("Cow", "Goat", "Sheep");

echo count($animals)//Output is 3

?>

How to Loop Through an Indexed Array

For loop can be used to loop through indexed array and print values in the array

Example

<?php

$animals= array("Cow", "Goat", "Sheep");

$arrayLength = count($animals);

 

for($r = 0; $r < $arrayLength; $r++) {

echo $animals[$r];

echo "<br>";

}

?>

PHP Associative Arrays

These are arrays that use named keys  assigned to them when they are created.

Just like Indexed Arrays,Associative arrays can be created in two ways.

$marks = array(“Eric”=>”70”, “Jane”=>”80”, “Mellisa”=>”75”);

or

$marks[‘Eric’] = “70”;
$marks[‘Jane’] = “80”;
$marks[‘Mellisa’] = “75”;

 

Example

<?php

$marks=array("Eric"=>"70","Jane"=>"80","Mellisa"=>"75");

echo "Eric Mark is  " . $marks['Eric'];

?>

How to Loop Through an Associative Array

Foreach loop can be used to loop through associative array.

<?php

$marks=array("Eric"=>"70","Jane"=>"80","Mellisa"=>"75");

 

foreach($marks as $y => $y_value) {

echo "Named Key is=" . $y . ", Value=" . $y_value;

echo "<br>";

}

?>

 

About the Author: Eric K

My name is Eric.Am a software developer with skills in Php,Mysql, Java , Android,Node.js Above all, I have certification in Cisco Networking Associate (CCNA) and holds degree in Information Technology. Hire me: Email : kawi3462@gmail.com