0% found this document useful (0 votes)
6 views1 page

2.1 ARRAY What Is An Array?: Example

An array is a variable that can hold multiple values, allowing for easier management of lists, such as car names. In PHP, arrays can be created using the array() function and come in three types: indexed arrays, associative arrays, and multidimensional arrays. Indexed arrays can be created with automatic or manual indexing, enabling efficient access to stored values.

Uploaded by

srishtisonawane6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

2.1 ARRAY What Is An Array?: Example

An array is a variable that can hold multiple values, allowing for easier management of lists, such as car names. In PHP, arrays can be created using the array() function and come in three types: indexed arrays, associative arrays, and multidimensional arrays. Indexed arrays can be created with automatic or manual indexing, enabling efficient access to stored values.

Uploaded by

srishtisonawane6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

11:29 (o) € | W

2.1 ARRAY
What is an Array?

An array is a special variable, Which can hold more than one value at a time
If you have a list of Items (a list of car names, for example), storing the cars in single variables
could look like this:
$carsl « "Volvo’’;
$cars2 = "BMW;
Scars 3 = Toyota";
However, what if you want to loop through the cars and find a specific one? And what if you had
not 3 cars, but 300?
The solution is to create an array!
An array can hold many values under a single name, and you can access the values by referring
to an index number

Create an Array in PHP


'In FHf’zihe array() fliimction is used to create an array:
In PHP, there are three types of arrays:
. iindexediferays • Arrays with a numeric index
. Assodaitive arrays - Arrays with named keys
. Multidimensional arrays - Arrays containing one or more arrays

PHP Indexed Arrays


Their e are two ways tp,create indexed arrays:
The index can be assigned automatically (index always starts at 0), like this:
Scars = arrayf Volvo", MBMWM, "Toyota":; t

or the index can be assigned manually:


$cars[0|« “Volvo";
$cars[l] =
Scars|2J = Toyota";
The following example creates an indexed array named Scars, assigns three elements to it, and
then prints a text containing the array values:

Example
<?php
Scars = array("Volvo", “BMW", “Toyota");
echo "1 like " $cars[0J " Scars) 1 j “ and" ScarsR] "
?>

You might also like