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

Arrays in PHP

PHP arrays are data structures that store multiple elements under a single variable, with three types: indexed, associative, and multidimensional arrays. Indexed arrays use numeric indices, associative arrays use named keys, and multidimensional arrays can contain other arrays, allowing for complex data storage. Examples demonstrate how to create and access elements in each type of array.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Arrays in PHP

PHP arrays are data structures that store multiple elements under a single variable, with three types: indexed, associative, and multidimensional arrays. Indexed arrays use numeric indices, associative arrays use named keys, and multidimensional arrays can contain other arrays, allowing for complex data storage. Examples demonstrate how to create and access elements in each type of array.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

PHP Arrays:

- Arrays in PHP are a type of data structure that allows us to store multiple
elements of similar or different data types under a single variable.

PHP Array Types:

In PHP, there are three types of arrays:

1. Indexed arrays - Arrays with a numeric index


2. Associative arrays - Arrays with named keys
3. Multidimensional arrays - Arrays containing one or more arrays

1. PHP Indexed Arrays

- In indexed arrays each item has an index number.

- By default, the first item has index 0, the second item has item 1, etc.

Example:

<?php

// One way to create an indexed array


$arr = array("aaa", "bbb");

// Accessing the elements directly


echo $arr[0], "<br>";
echo $arr[1], "<br>";

// Second way to create an indexed array


$arr[0] = "ABC";
$arr[1] = "XYZ";

// Accessing the elements directly


echo "Accessing the 2nd array elements directly:<br>";
echo $arr[0], "<br>";
echo $arr[1], "<br>";

?>

Output:

aaa
bbb
Accessing the 2nd array elements directly:
ABC
XYZ

PHP Associative Arrays:

- Associative arrays are arrays that use named Keys & Values that you assign to
them.
Ex:

<html>
<body>
<pre>

<?php
$car = array("College"=>"NHC", "Class"=>"BCA", "SNO"=>420);
var_dump($car);
?>

</pre>
</body>
</html>

Output:

array(3)
{
["College"]=>
string(3) "NHC"
["Class"]=>
string(3) "BCA"
["SNO"]=>
int(420)
}

3. Multidimensional Arrays:

- we can define multi-dimensional arrays as an array of arrays.


- As the name suggests, every element in this array can be an array and they can
also hold other sub-arrays within.
- Arrays or sub-arrays in multidimensional arrays can be accessed using multiple
dimensions.

- It allows you to store tabular data in an array.


- PHP multidimensional array can be represented in the form of matrix

<?php
$tbl = [ [1,2,3,4],
[10, 20, 30, 40],
[100, 200, 300, 400]
];

# prints number in index 2 of the row 2


print ("Value at [2], [2] :" . $tbl[2][2]);
?>

OUTPUT:
Value at [2], [2] :300

EX:

<?php
$students = array(
array("name"=>"aaa", "age"=>39),
array("name"=>"bbb", "age"=>49),
array("name"=>"ccc", "age"=>59)
);

echo $students[0]["name"];
echo $students[0]["age"] . "<br>";

echo $students[1]["name"];
echo $students[1]["age"] . "<br>";

echo $students[2]["name"];
echo $students[2]["age"];
?>

output:
aaa39
bbb49
ccc59

You might also like