[go: up one dir, main page]

0% found this document useful (0 votes)
4 views12 pages

ArrayinPHPpptx 2024 10 17 19 09 22

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 12

Array in PHP

An array is a data structure that stores one or more data values having
some relation among them, in a single variable. For example, if you
want to store the marks of 10 students in a class, then instead of
defining 10 different variables, it’s easy to define an array of 10 length.
Arrays in PHP behave a little differently than the arrays in C, as PHP is a
dynamically typed language as against C which is a statically type
language.
• An array in PHP is an ordered map that associates values to keys.
• A PHP array can be used to implement different data structures such
as a stack, queue, list (vector), hash table, dictionary, etc.
• The value part of an array element can be other arrays. This fact can
be used to implement tree data structure and multidimensional
arrays.
There are two ways to declare an array in PHP. One is to use the built-in
array() function, and the other is to use a shorter syntax where the
array elements are put inside square brackets.
array() Function: The built-in array() function uses the parameters given
to it and returns an object of array type. One or more comma-
separated parameters are the elements in the array.
array(mixed ...$values)
Each value in the parenthesis may be either a singular value (it may be
a number, string, any object or even another array), or a key-value pair.
The association between the key and its value is denoted by the "=>"
symbol.
Examples:
• $arr1 = array(10, "asd", 1.55, true);
• $arr2 = array("one"=>1, "two"=>2, "three"=>3);
• $arr3 = array(array(10, 20, 30), array("Ten", "Twenty", "Thirty"),
array("physics"=>70, "chemistry"=>80, "maths"=>90));
2. Using Square Brackets [ ]: Instead of the array() function, the comma-
separated array elements may also be put inside the square brackets to
declare an array object. In this case too, the elements may be singular
values or a string or another array.

• $arr1 = [10, "asd", 1.55, true];


• $arr2 = ["one"=>1, "two"=>2, "three"=>3];
• $arr3 = [ [10, 20, 30], ["Ten", "Twenty", "Thirty"], ["physics"=>70,
"chemistry"=>80, "maths"=>90] ];
Types of Arrays in PHP: There are three different kind of arrays and
each array value is accessed using an ID which is called the array index.
• Indexed Array − An array which is a collection of values only is called
an indexed array. Each value is identified by a positional index staring
from "0". Values are stored and accessed in linear fashion.
• Associative Array − If the array is a collection of key-value pairs, it is
called as an associative array. The key component of the pair can be a
number or a string, whereas the value part can be of any type.
Associative arrays store the element values in association with key
values rather than in a strict linear index order.
• Multi Dimensional Array − If each value in either an indexed array or
an associative array is an array itself, it is called a multi dimensional
array. Values are accessed using multiple indices
Example-1
<?php array(4) {
$arr1 = [10, "asd", 1.55, true]; [0]=>
var_dump($arr1); int(10)
[1]=>
?>
string(3) "asd"
[2]=>
float(1.55)
[3]=>
bool(true)
}
Example-2
<?php • ?????
$arr1 = [
[10, 20, 30],
["Ten", "Twenty", "Thirty"],
[1.1, 2.2, 3.3]
];
var_dump($arr1);
?>
Example-3
<?php
$arr1 = [10, 20, 30];
$arr2 = array("one"=>1,
"two"=>2, "three"=>3);

var_dump($arr1[1]);
var_dump($arr2["two"]);
?>
Example-4
<?php
$numbers = array(10, 20, 30, 40,
50);

for ($i=0; $i<count($numbers); $i+


+){
echo "numbers[$i] =
$numbers[$i] \n";
}
?>
Example-5
<?php
$numbers = array(10, 20, 30, 40,
50);
$i = count($numbers)-1;
while ($i>=0){
echo "numbers[$i] =
$numbers[$i] \n";
$i--;
}
?>

You might also like