In this article Arrays in PHP we give the information about arrays are a way of storing data. It allows storing more than one value in the same variable. Arrays in PHP are used to organize and manage data.
Arrays in PHP:
Types of Arrays in PHP
There are mainly three types of arrays in PHP:
- Indexed Array: Which contains only values and the index is automatically numbered (starting from 0).
- Associative Array: Which contains keys and values.
- Multidimensional Array: Which contains arrays within arrays.
- Indexed Array
It stores values based on index number.
Ways to create Indexed Array:
<?php
// Method 1: Using Array Function
$fruits = array(“Apple”, “Banana”, “Orange”);
// Method 2: Short syntax (PHP 5.4+)
$fruits = [“apple”, “banana”, “orange”];
//printing the value
echo $fruits[0]; // Apple
?>
Using Indexed Array in Loop
<?php
$fruits = [“apple”, “banana”, “orange”];
foreach ($fruits as $fruit) {
echo “Fruit: $fruit<br>”;
}
?>
Output:
Fruit: Apple
Fruit: Banana
Fruit: Orange
- Associative Array
It stores data in key-value pairs.
Method of making Associative Array:
<?php
$marks = [
“Rahul” => 85,
“Sonia” => 90,
“peace” => 75
];
//printing the value
echo “Rahul’s marks: ” .$marks[“Rahul”]; //85
?>
Using Associative Array in Loop
<?php
$marks = [
“Rahul” => 85,
“Sonia” => 90,
“peace” => 75
];
foreach ($marks as $name => $mark) {
echo “Student: $name, Marks: $mark<br>”;
}
?>
<?php
$marks = [
“Rahul” => 85,
“Sonia” => 90,
“peace” => 75
];
foreach ($marks as $name => $mark) {
echo “Student: $name, Marks: $mark<br>”;
}
?>
Output:
Student: Rahul, Marks: 85
Student: Sonia, Marks: 90
Student: Aman, Marks: 75
-
Multidimensional Array
It stores arrays within arrays.
Method of creating Multidimensional Array:
<?php
$students = [
[“name” => “Rahul”, “number” => 85],
[“name” => “Sonia”, “number” => 90],
[“name” => “aman”, “number” => 75]
];
//accessing data
echo $students[0][“name”]; // A male name
?>
Using Multidimensional Array in a Loop
<?php
$students = [
[“name” => “Rahul”, “number” => 85],
[“name” => “Sonia”, “number” => 90],
[“name” => “aman”, “number” => 75]
];
foreach ($students as $student) {
echo “Student: ” .$student[“name”]. “, score: ” . $student[“marks”] . “<br>”;
}
?>
Output:
Student: Rahul, Marks: 85
Student: Sonia, Marks: 90
Student: Aman, Marks: 75
Useful Functions of Arrays in PHP
1.count()
Counts the number of items in the array.
<?php
$fruits = [“apple”, “banana”, “orange”];
echo count($fruits); //3
?>
- array_push()
Adds a new value to the array.
<?php
$fruits = [“apple”, “banana”];
array_push($fruits, “Orange”);
print_r($fruits); // [“apple”,”banana”,”orange”]
?>
- array_pop()
Removes the last value from the array.
<?php
$fruits = [“apple”, “banana”, “orange”];
array_pop($fruits);
print_r($fruits); // [“apple”,”banana”]
?>
- array_merge()
Merges two or more arrays.
<?php
$array1 = [“apple”, “banana”];
$array2 = [“Orange”, “Mango”];
$result = array_merge($array1, $array2);
print_r($result); // [“apple”,”banana”,”orange”,”mango”]
?>
- in_array()
Checks if a value exists in the array.
<?php
$fruits = [“apple”, “banana”, “orange”];
if (in_array(“apple”, $fruits)) {
echo “Apple exists in array.”;
}
?>
- array_keys() and array_values()
array_keys(): Gets all the keys.
array_values(): Gets all the values.
<?php
$marks = [“Rahul” => 85, “Sonia” => 90];
print_r(array_keys($marks)); // [“Rahul”,”Sonia”]
print_r(array_values($marks)); // [85, 90]
?>
Conclusion
- Arrays are used to organize and store data efficiently.
- Indexed, Associative, and Multidimensional Arrays can handle different data types in your application.
- PHP Array Functions help to manage and process data easily.
PHP Array Functions and Sorting
Array Functions and Sorting in PHP are used to manage and organize Arrays. These allow you to change, add, remove, and sort the contents of arrays.
PHP Array Functions
- array_merge()
Merges two or more arrays.
<?php
$array1 = [“apple”, “banana”];
$array2 = [“Orange”, “Mango”];
$result = array_merge($array1, $array2);
print_r($result);
// [“apple”,”banana”,”orange”,”mango”]
?>
- array_push() and array_pop()
- array_push(): Appends one or more values to the end of the array.
- array_pop(): Removes the last value of the array.
<?php
$fruits = [“apple”, “banana”];
array_push($fruits, “Orange”, “Mango”);
print_r($fruits); // [“apple”,”banana”,”orange”,”mango”]
array_pop($fruits);
print_r($fruits); // [“apple”,”banana”,”orange”]
?>
- array_unshift() and array_shift()
- array_unshift(): Appends the value to the beginning of the array.
- array_shift(): Removes the first value from the array.
<?php
$fruits = [“banana”, “orange”];
array_unshift($fruits, “apple”);
print_r($fruits); // [“apple”,”banana”,”orange”]
array_shift($fruits);
print_r($fruits); // [“banana”,”orange”]
?>
-
array_unique()
Removes duplicate values from the array.
<?php
$numbers = [1, 2, 2, 3, 4, 4];
$result = array_unique($numbers);
print_r($result); // [1, 2, 3, 4]
?>
- in_array()
Checks if a value exists in the array.
<?php
$fruits = [“apple”, “banana”, “orange”];
if (in_array(“banana”, $fruits)) {
echo “Banana exists in array.”;
}
?>
- array_keys() and array_values()
- array_keys(): Returns all keys.
- array_values(): Returns all values.
<?php
$marks = [“Rahul” => 85, “Sonia” => 90];
print_r(array_keys($marks)); // [“Rahul”,”Sonia”]
print_r(array_values($marks)); // [85, 90]
?>
-
array_reverse()
Returns the array in reverse order.
<?php
$fruits = [“apple”, “banana”, “orange”];
$result = array_reverse($fruits);
print_r($result); // [“orange”,”banana”,”apple”]
?>
- array_search()
Finds a value in an array and returns its key.
<?php
$fruits = [“apple”, “banana”, “orange”];
$key = array_search(“Banana”, $fruits);
echo “Banana key: $key”; //1
?>
PHP Array Sorting
Sorting Functions
Function Name Description
sort() sorts the array in ascending order by value.
rsort() sorts the array in descending order by value.
asort() sorts by value, preserving the keys.
arsort() sorts the values in descending order, preserving the keys.
ksort() sorts the keys in ascending order.
krsort() sorts the keys in descending order.
- sort() and rsort()
<?php
$numbers = [4, 2, 8, 1];
sort($numbers);
print_r($numbers); // [1, 2, 4, 8]
rsort($numbers);
print_r($numbers); // [8, 4, 2, 1]
?>
- asort() and arsort()
<?php
$marks = [“Rahul” => 85, “Sonia” => 90, “Aman” => 75];
assort($marks); // ascending order by value
print_r($marks);
arsort($marks); // descending order by value
print_r($marks);
?>
-
ksort() and krsort()
<?php
$marks = [“Rahul” => 85, “Sonia” => 90, “Aman” => 75];
ksort($marks); // ascending order by keys
print_r($marks);
krsort($marks); // descending order by keys
print_r($marks);
?>
- Custom Sorting (usort, uasort, uksort)
Use for custom sorting.
<?php
$numbers = [3, 1, 4, 2];
// custom sorting
usort($numbers, function($a, $b) {
return $a <=> $b; // ascending order
};
print_r($numbers); // [1, 2, 3, 4]
?>
Conclusion
- PHP Array Functions help you manage and process Arrays.
- Sorting Functions make data organized and efficient.
- Custom Sorting can be used for specific needs.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining