0% found this document useful (0 votes)
8 views24 pages

Chapter 4 Arrays in PHP

Uploaded by

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

Chapter 4 Arrays in PHP

Uploaded by

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

Chapter 4 Arrays in PHP

Chapter 4 Arrays in PHP


Introduction
• An array is a collection of data values.
• Array is organized as an ordered collection of
key-value pairs.
• An array is a special variable, which can store
multiple values in one single variable.
• An array can hold all your variable values under a
single name and you can access the values by
referring to the array name.
• Each element in the array has its own index so
that it can be easily accessed.
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:
• $cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";
• In PHP, the array() function is used to create an
array: array()
Types of Array
In PHP, there are three types of arrays:
• Indexed/Numeric arrays - Arrays with
a numeric index
• Associative arrays - Arrays with named
keys
• Multidimensional arrays - Arrays containing
one or more arrays
PHP Indexed
Arrays
• There are two ways to create indexed arrays:
• The index can be assigned automatically (index
always starts at 0), like this:
• Initializing indexed array
•$cars = array("Volvo", "BMW",
"Toyota"); or
• the index can be assigned manually:
• $cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
• Print_r($cars) - used to print array and used for
testing purpose
Two Ways to Create Indexed Arrays in
PHP:
1. Automatically Indexed
<?php
$cars = array("Volvo", "BMW", "Toyota");

echo $cars[0]; // Outputs: Volvo


echo $cars[1]; // Outputs: BMW
echo $cars[2]; // Outputs: Toyota
?>
Output
VolvoBMWToyota
2. Manually Indexed

<?php
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";

print_r($cars); // For testing


?>
Output
Array ( [0] => Volvo [1] => BMW [2] => Toyota )
Adding value to the end of an
array
<?php
$colors=array("red",24,"blue",33.66);
echo "<ul>";
$colors[]="pink";
for($i=0;$i<=4;$i++)
echo
"<li>$colors[$i]
</li>";
echo "</ul>";
?>
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
key=Value
• $age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
For each loop
Syntax: foreach($array as $value)
PHP executes the body of the loop once for each
element of $array, with $value set to the current
elements. Elements are processed by their
internal order

Storing data in array
To store the data in array, the array should be defined before. But if it
is
not defined , then it is created automatically.
• Trying to access the members of array which does not exist is not
allowed.
• Simple assignment to initialize an array
• Indexed array:
$a[0]=10;
$a[1]=20;
$a[2]=30;
• For Associative array:
$a[‘one’]=1;
$a[‘two’]=2;
$a[‘three’]=3;
• To initialize an array, array() construct can be used.
• Indexed array:
$number=array(10,20,30);
$cities=array(‘Delhi’,’Mumbai’,’Chennai’);
• For Associative array:
$number=array(‘one’=>100,‘two’=>200,‘three’=>300);
Storing data in array
• To create an empty array:
$number=array();
• You can specify an initial key with => and then
a list of values. The values are inserted into the
array starting with that key, with subsequent
values in the sequence. But the initial key is the
string then next keys cannot be continued with
the next value of that string key, so the next key
numbering start with 0;
• $cities=array(1=>’delhi’,’Mumbai’,’Kolkata’,’
Chennai’);
• This associate key 1 with the value ‘delhi’ and
assigs value 2 to Mumbai 3 to Kolkata and so
on
Adding values to the end of array
• To insert more values at the end of array use[]
syntax.
E.g
• Indexed array:
$A=array(1,2,3); $A[]=4;- adds value at the end of
the array it gets the numeric index 3.
• Associative array:
$A=array(‘one’=>1, ’two’=>2, ’three’=>3);
$A[]=4;// $A[0]=4; - adds value at the end of the array
but it gets the numeric index 0.
Assigning a Range of Values
• The range() function creates an array of
consecutive integer or character between
two values we pass to it as a parameter.
• $number=range(2,5)
• //$number= array(2,3,4,5);
• $letter=range(‘a’, ’d’);
• //letter=array(‘a’, ’b’ ,’c’, ’d’);
Getting size of an array
• The count() functions are used to return number of
elements in the array.
Syntax: count($array );
$a= array(1,2,3,4);
$size=count($a); //count 4
• The sizeof() function is an alias of the count()
function.
$a= array(1,2,3,4);
$size=sizeof($a); //size 4
Padding an array
• array_pad() function is used for padding an array.
• Syntax:
$array=array_pad(array $array, int $size, mixed
$value);
• array_pad() function returns a copy of the ‘array’ padded
to size specified by ‘size’ with value ‘$value’.
• If size is positive then the array is padded on the right.
• If size is negative then it is padded on left.
$a=array(1,2,3);
$pad=array_pad($a, 5, 0); // $pad= array(1, 2, 3, 0, 0);
$pad=array_pad($a, -5, 0); // $pad= array(0,0,1, 2, 3);
Multidimensional Arrays
• A multidimensional array is an array containing one
or more arrays.
• Two dimensional array:
• $cars = array
(
array("Volv
o",22,18),
array("BM
W",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
Extracting Multiple Values:
• To copy an array’s values into variables use the list()
construct.
• Syntax: list($var1, $var2, …….)=$array
e.g. $a=array(1, 2, 3);
list($x, $y, $z)=$a; // $x=1, $y=2, $z=3
• If array has more values than listed variables, then
extra values are ignored.
•E.g.$a=array(1,2,3);
List($x, $y)=$a; //$x=1, $y=2
Slicing an
• To extract a slice of the array, use
Array
array_slice() function.
Example: PHP Array Slicing
<?php
$fruits = array("apple", "banana", "cherry", "date", "fig");

// Slice from index 1 (second element), get 3 items


$sliced = array_slice($fruits, 1, 3);

print_r($sliced);
?>

Output:
Array
(
[0] => banana
[1] => cherry
[2] => date
)
Converting between Arrays
and Variables
• PHP provides two functions extract() and
compact() that convert between arrays and
variables.
• extract(): extract() function
automatically creates local variables from
an array.
• compact():The compact() function creates
an array from variables and their values.
Traversing Arrays
• foreach loop
• for loop
• The iterator functions: Every PHP array keep track of the current element. The pointer to
the current element is known as the iterator.
• Iterator functions are :
1. current(): Returns the currently pointed element.
2. reset(): Moves the iterator to the first element in the array and returns it.
3.next():Moves the iterator to the next element in the array and returns it.
4.prev():Moves the iterator to the previous element in the array and returns it.
5.end():Moves the iterator to the last element in the array and returns it.
Conclusion

Arrays in PHP help manage data efficiently using


different types: indexed, associative, and
multidimensional. Mastering arrays is key to
building dynamic and data-driven PHP
applications.
THANKS!

You might also like