In This Blog, you are going to learn Arrays in php and also types of arrays in php. Array is one of the most important concept in php programming.
PHP Arrays
- An array is a variable that can store a list of values referred by the same name.
- An array stores multiple values in one single variable.
- An array is a special variable, which can hold more than one value at a time
Types of arrays are as follows:
- Single-dimensional Arrays
- Indexed Arrays
- Associative Arrays
- Multi-dimensional Arrays
Arrays In PHP
- Array is a variable that can store a set of values of the same data type or different data type.
- Each element of an array can be referred by an array name and an index
Array index
- Is used to access an element
- Can be a number or a string
Important Points
- If index is number, then array is an indexed array
- If index is string, then array is an associative array
- By default, the index value in an array starts at zero
Initializing an Array In PHP
- Two ways of initializing an array are as follows:
- array() function – assigns value to all the elements of an array
- array identifier – assigns value to a specific element of an array
SINGLE DIMENSIONAL ARRAY

ASSOCIATVE ARRAYS IN PHP
- Associative array uses key-value pairs separated by a comma to create an array
- Associative array is an array where the index type is a string
- The index value must be specified within double quotes
- If index is string, then array is an associative array
Indexed Arrays In PHP
- Includes an integer as the index type
- By default, PHP creates an indexed array, if the index type is not specified at the time of creating an array
- The index value can start with any integer, such as 1, 20, or 123.
- Uses key-value pairs separated by a comma to create an array.
Multi-dimensional Arrays In PHP
- Contains one array stored within another.
- A multidimensional array is an array containing one or more arrays.
- A multidimensional array is an array of arrays.
- In a multi-dimensional array, each element is an array.
- Each element requires an array name and multiple set of indices.

Array-Related Functions
- sort() Function
- rsort() Function
- Asort() Function
- Ksort() Function
- Arsort() Function
- Krsort() Function
- array_merge() function
- count() function
sort() Function
Arranges the element values in alphabetical / ascending order.
rsort() Function
Sorts the element values in descending alphabetical order.
ASORT() Function
sort associative arrays in ascending order, according to the value.
KSORT() FUNCTION
sort associative arrays in ascending order, according to the key
arsort() Function
sort associative arrays in descending order, according to the value
KRSORT() FUNCTION
sort associative arrays in descending order, according to the key
Merging Arrays
The array_merge() function is used to combine the element values of two or more arrays
count() Function
The count() function is used to return the length (the number of elements) of an array.
Source Code
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php
// $bio_data = array(
//
// array("Ali",23,5.9),
// array("Zain",24,5.8),
// array("Osama",26,5.7)
//
// );
//
// for($i = 0; $i < 3; $i++)
// {
//
// for($j = 0; $j < 3; $j++)
// {
// echo $bio_data[$i][$j] . "<br>";
// }
// }
// echo $bio_data;
// echo $bio_data[1][2];
// $dept_Array = array(10 => "Finance", 20 => "H-R", 30 => "Accounts");
//
// echo $dept_Array[20];
//$Departments = array("c"=>"H-R","a"=>"Finance","d"=>"Counselling", "b"=>"Accounts");
// asort($Departments);
// ksort($Departments);
// arsort($Departments);
//krsort($Departments);
// foreach($Departments as $k => $v)
// {
// echo "Key is $k and Value is $v <br>";
//// echo "Value is " . $value . " Hi Mustafa <br>";
// }
// echo $Departments["b"];
$names1 = array("Bilal","Anas","Zain","Osama","Amir");
if(count($names1) > 10)
{
echo "Hello";
}
else
{
echo "Bye";
}
// echo count($names1);
// $names2 = array("Danish","Aziz","Faheem","Shadab","Usman");
// $numbers = array(11,22,66,44,33,66);
//
// $all_names = array_merge($names1, $numbers, $names2);
//
// foreach($all_names as $name)
// {
// echo $name . "<br>";
// }
//
//// sort($names); // ascending
// rsort($names); // descending
//
// foreach($names as $name)
// {
// echo $name . "<br>";
// }
// $names[0] = "Ali";
// $names[1] = "Anas";
// $names[2] = "Zain";
// $names[3] = "Amir";
//
// foreach($names as $name)
// {
// echo $name . "<br>";
// }
// $length = count($names);
// for($i = 0; $i < count($names); $i++)
// {
// echo $names[$i] . "<br>";
// }
// echo $names[0] . " " . $names[1] . " " . $names[2] . " " . $names[3];
?>
</body>
</html>
Download Notes Of This Blog From the link given below
https://www.mediafire.com/file/b6cr5zx5f2b4d7h/WORKING+WITH+ARRAYS.pptx/file
Download Source Code of This Blog From the link given below
https://www.mediafire.com/file/zftowm2um652q8g/Arrays.php/file
No responses yet