Arrays are awesome, but what if you need to add, remove, sort, or modify them? Welcome to array manipulation in PHP!
Adding Elements to an Array
Using array_push()
Adds one or more elements to the end of an array.
$fruits = ["Apple", "Banana"];
array_push($fruits, "Cherry", "Orange");
print_r($fruits);
// Output: Array ( [0] => Apple [1] => Banana [2] => Cherry [3] => Orange )
Using array_unshift()
Adds elements to the beginning of an array.
$fruits = ["Banana", "Cherry"];
array_unshift($fruits, "Apple");
print_r($fruits);
// Output: Array ( [0] => Apple [1] => Banana [2] => Cherry )
Removing Elements from an Array
Using array_pop()
Removes the last element from an array.
$fruits = ["Apple", "Banana", "Cherry"];
array_pop($fruits);
print_r($fruits);
// Output: Array ( [0] => Apple [1] => Banana )
Using array_shift()
Removes the first element from an array.
$fruits = ["Apple", "Banana", "Cherry"];
array_shift($fruits);
print_r($fruits);
// Output: Array ( [0] => Banana [1] => Cherry )
Sorting an Array
Sorting in Ascending Order (sort()
)
$numbers = [3, 1, 4, 2];
sort($numbers);
print_r($numbers);
// Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )
Sorting in Descending Order (rsort()
)
rsort($numbers);
print_r($numbers);
// Output: Array ( [0] => 4 [1] => 3 [2] => 2 [3] => 1 )
Sorting Associative Arrays (asort()
& ksort()
)
$ages = ["Alice" => 25, "Bob" => 22, "Charlie" => 30];
asort($ages); // Sort by values
print_r($ages);
// Output: Array ( [Bob] => 22 [Alice] => 25 [Charlie] => 30 )
ksort($ages); // Sort by keys
print_r($ages);
// Output: Array ( [Alice] => 25 [Bob] => 22 [Charlie] => 30 )
Filtering an Array
Using array_filter()
Filters an array based on a callback function.
$numbers = [1, 2, 3, 4, 5, 6];
$evenNumbers = array_filter($numbers, function($num) {
return $num % 2 == 0;
});
print_r($evenNumbers);
// Output: Array ( [1] => 2 [3] => 4 [5] => 6 )
Merging Arrays
Using array_merge()
Combines two or more arrays.
$array1 = ["a", "b", "c"];
$array2 = ["d", "e", "f"];
$mergedArray = array_merge($array1, $array2);
print_r($mergedArray);
// Output: Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f )
Searching in an Array
Using in_array()
Checks if a value exists in an array.
$fruits = ["Apple", "Banana", "Cherry"];
if (in_array("Banana", $fruits)) {
echo "Banana is in the array!";
}
// Output: Banana is in the array!
Using array_search()
Finds the index/key of a value.
$fruits = ["Apple", "Banana", "Cherry"];
$key = array_search("Cherry", $fruits);
echo $key;
// Output: 2
Real-World Example: To-Do List
$tasks = ["Buy groceries", "Finish PHP project", "Call mom"];
// Add a task
array_push($tasks, "Exercise");
// Remove completed task
array_shift($tasks);
// Print updated list
print_r($tasks);
// Output: Array ( [0] => Finish PHP project [1] => Call mom [2] => Exercise )
PHP array manipulation is super powerful and helps you manage data efficiently! Now, you can: Add and remove elements. Sort and filter arrays. Search and merge arrays. Apply these in real-world scenarios!
0 Comments