0% found this document useful (0 votes)
38 views

PHP Array

This document discusses PHP associative arrays, which use named keys to store values. There are two ways to create an associative array: by assigning values to keys in an array declaration, or by assigning values to keys individually. Associative array keys can then be used to access and output the corresponding values. A foreach loop can also be used to loop through all the keys and values in an associative array. The document then provides a link to more complete documentation on PHP arrays.

Uploaded by

Muhammad Alek
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

PHP Array

This document discusses PHP associative arrays, which use named keys to store values. There are two ways to create an associative array: by assigning values to keys in an array declaration, or by assigning values to keys individually. Associative array keys can then be used to access and output the corresponding values. A foreach loop can also be used to loop through all the keys and values in an associative array. The document then provides a link to more complete documentation on PHP arrays.

Uploaded by

Muhammad Alek
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

PHP Associative Arrays

❮ PreviousNext ❯

PHP Associative Arrays


Associative arrays are arrays that use named keys that you assign to them.

There are two ways to create an associative array:

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

or:

$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";

The named keys can then be used in a script:

Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
Try it Yourself »

Loop Through an Associative Array


To loop through and print all the values of an associative array, you could use
a foreach loop, like this:

Example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $x_value) {


echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
Try it Yourself »

Complete PHP Array Reference


For a complete reference of all array functions, go to our complete PHP Array
Reference.

The reference contains a brief description, and examples of use, for each
function!

PHP Exercises
Test Yourself With Exercises
Exercise:
Create an associative array containing the age of Peter, Ben and Joe.

$age = array("Peter" "35", "Ben" "37", "Joe"


"43");

Source : https://www.w3schools.com/php/php_arrays_associative.asp

You might also like