PHP Array Methods
Array Creation and Initialization
Using Array Literal Syntax:
Creates an array using array literal syntax, where elements are enclosed in square brackets.
$array = [1, 2, 3, 4, 5];
print_r($array);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Using the array() Function:
Creates an array using the array() function, specifying elements as arguments.
$array = array(1, 2, 3, 4, 5);
print_r($array);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Using the range() Function:
Creates an array containing a range of elements using the range() function.
$array = range(1, 5);
print_r($array);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Using Short Array Syntax for Associative Arrays:
Creates an associative array using short array syntax, where keys and values are specified within square brackets.
$array = ['a' => 1, 'b' => 2, 'c' => 3];
print_r($array);
Output:
Array
(
[a] => 1
[b] => 2
[c] => 3
)
Using the array_fill() Function:
Creates an array filled with a specified value using the array_fill() function.
$array = array_fill(0, 5, 'Hello');
print_r($array);
Output:
Array
(
[0] => Hello
[1] => Hello
[2] => Hello
[3] => Hello
[4] => Hello
)
Modification and Manipulation
array_pop():
Removes and returns the last element of an array using the array_pop() function.
$array = [1, 2, 3, 4, 5];
$lastElement = array_pop($array);
print_r($array);
echo "Popped element: " . $lastElement;
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
Popped element: 5
array_push():
Adds one or more elements to the end of an array using the array_push() function.
$array = [1, 2, 3, 4];
array_push($array, 5, 6);
print_r($array);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
array_shift():
Removes and returns the first element of an array using the array_shift() function.
$array = [1, 2, 3, 4, 5];
$firstElement = array_shift($array);
print_r($array);
echo "Shifted element: " . $firstElement;
Output:
Array
(
[0] => 2
[1] => 3
[2] => 4
[3] => 5
)
Shifted element: 1
array_unshift():
Adds one or more elements to the beginning of an array using the array_unshift() function.
$array = [2, 3, 4, 5];
array_unshift($array, 1);
print_r($array);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
array_splice():
Removes and/or replaces elements of an array using the array_splice() function.
$array = [1, 2, 3, 4, 5];
array_splice($array, 2, 2, ['a', 'b']); // Replace 2 elements starting from index 2 with 'a' and 'b'
print_r($array);
Output:
Array
(
[0] => 1
[1] => 2
[2] => a
[3] => b
[4] => 5
)
array_pad():
Pads an array to a specified length with a specified value using the array_pad() function.
$array = [1, 2, 3];
$array = array_pad($array, 5, 0); // Pad array to the specified length with a value
print_r($array);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 0
[4] => 0
)
array_fill_keys():
Fills an array with values, specifying keys using another array using the array_fill_keys() function.
$keys = ['a', 'b', 'c'];
$array = array_fill_keys($keys, 'value');
print_r($array);
Output:
Array
(
[a] => value
[b] => value
[c] => value
)
array_reverse():
Returns an array with elements in reverse order using the array_reverse() function.
$array = [1, 2, 3, 4, 5];
$reversedArray = array_reverse($array);
print_r($reversedArray);
Output:
Array
(
[0] => 5
[1] => 4
[2] => 3
[3] => 2
[4] => 1
)
array_flip():
Exchanges all keys with their associated values in an array using the array_flip() function.
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$flippedArray = array_flip($array);
print_r($flippedArray);
Output:
Array
(
[1] => a
[2] => b
[3] => c
)
PHP Array Combining and Merging
array_merge():
Merges one or more arrays into one.
$array1 = [1, 2, 3];
$array2 = ['a', 'b', 'c'];
$mergedArray = array_merge($array1, $array2);
print_r($mergedArray);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => a
[4] => b
[5] => c
)
array_merge_recursive():
Merges two or more arrays into one recursively.
$array1 = ['a' => ['x'], 'b' => ['y']];
$array2 = ['a' => ['z'], 'b' => ['w']];
$mergedArray = array_merge_recursive($array1, $array2);
print_r($mergedArray);
Output:
Array
(
[a] => Array
(
[0] => x
[1] => z
)
[b] => Array
(
[0] => y
[1] => w
)
)
array_combine():
Creates an array by using one array for keys and another for its values.
$keys = ['a', 'b', 'c'];
$values = [1, 2, 3];
$combinedArray = array_combine($keys, $values);
print_r($combinedArray);
Output:
Array
(
[a] => 1
[b] => 2
[c] => 3
)
PHP Array Information and Access
count():
Counts all elements in an array or something in an object.
$array = [1, 2, 3, 4, 5];
$count = count($array);
echo "Number of elements in the array: " . $count;
Output:
Number of elements in the array: 5
in_array():
Checks if a value exists in an array.
$array = [1, 2, 3, 4, 5];
$isPresent = in_array(3, $array);
echo "Is 3 present in the array? " . ($isPresent ? 'Yes' : 'No');
Output:
Is 3 present in the array? Yes
array_key_exists():
Checks if a specified key exists in the array.
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$exists = array_key_exists('b', $array);
echo "Does key 'b' exist in the array? " . ($exists ? 'Yes' : 'No');
Output:
Does key 'b' exist in the array? Yes
array_keys():
Returns all the keys or a subset of the keys of an array.
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$keys = array_keys($array);
print_r($keys);
Output:
Array
(
[0] => a
[1] => b
[2] => c
)
array_values():
Returns all the values of an array.
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$values = array_values($array);
print_r($values);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
array_column():
Returns the values from a single column in the input array.
$records = [
['id' => 1, 'name' => 'John'],
['id' => 2, 'name' => 'Jane'],
['id' => 3, 'name' => 'Doe']
];
$names = array_column($records, 'name');
print_r($names);
Output:
Array
(
[0] => John
[1] => Jane
[2] => Doe
)
array_rand():
Pick one or more random keys out of an array.
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$randomKey = array_rand($array);
echo "Random key: " . $randomKey;
Output:
Random key: b
array_key_first():
Get the first key of an array.
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$firstKey = array_key_first($array);
echo "First key: " . $firstKey;
Output:
First key: a
array_key_last():
Get the last key of an array.
$array = ['a' => 1, 'b' => 2, 'c' => 3];
$lastKey = array_key_last($array);
echo "Last key: " . $lastKey;
Output:
Last key: c
PHP Array Sorting
sort():
Sorts an array in ascending order.
$array = [3, 1, 5, 2, 4];
sort($array);
print_r($array);
Output:
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
rsort():
Sorts an array in descending order.
$array = [3, 1, 5, 2, 4];
rsort($array);
print_r($array);
Output:
Array
(
[0] => 5
[1] => 4
[2] => 3
[3] => 2
[4] => 1
)
asort():
Sorts an associative array in ascending order, according to the value.
$array = ['b' => 3, 'a' => 1, 'c' => 5];
asort($array);
print_r($array);
Output:
Array
(
[a] => 1
[b] => 3
[c] => 5
)
arsort():
Sorts an associative array in descending order, according to the value.
$array = ['b' => 3, 'a' => 1, 'c' => 5];
arsort($array);
print_r($array);
Output:
Array
(
[c] => 5
[b] => 3
[a] => 1
)
ksort():
Sorts an associative array in ascending order, according to the key.
$array = ['b' => 3, 'a' => 1, 'c' => 5];
ksort($array);
print_r($array);
Output:
Array
(
[a] => 1
[b] => 3
[c] => 5
)
krsort():
Sorts an associative array in descending order, according to the key.
$array = ['b' => 3, 'a' => 1, 'c' => 5];
krsort($array);
print_r($array);
Output:
Array
(
[c] => 5
[b] => 3
[a] => 1
)
natsort():
Sorts an array using a "natural order" algorithm.
$array = ['img10.png', 'img2.png', 'img1.png'];
natsort($array);
print_r($array);
Output:
Array
(
[2] => img1.png
[1] => img2.png
[0] => img10.png
)
natcasesort():
Sorts an array using a case-insensitive "natural order" algorithm.
$array = ['IMG10.png', 'img2.png', 'img1.png'];
natcasesort($array);
print_r($array);
Output:
Array
(
[2] => img1.png
[1] => img2.png
[0] => IMG10.png
)
PHP Array Iteration Functions
array_walk():
Apply a user-defined function to each element of an array.
$array = [1, 2, 3, 4, 5];
function myFunction(&$value, $key) {
$value *= 2;
}
array_walk($array, 'myFunction');
print_r($array);
Output:
Array
(
[0] => 2
[1] => 4
[2] => 6
[3] => 8
[4] => 10
)
array_map():
Applies a callback function to each element of an array and returns a new array with the results.
$array = [1, 2, 3, 4, 5];
function myFunction($value) {
return $value * 2;
}
$result = array_map('myFunction', $array);
print_r($result);
Output:
Array
(
[0] => 2
[1] => 4
[2] => 6
[3] => 8
[4] => 10
)
array_filter():
Filters elements of an array using a callback function.
$array = [1, 2, 3, 4, 5];
function myFunction($value) {
return $value % 2 == 0;
}
$result = array_filter($array, 'myFunction');
print_r($result);
Output:
Array
(
[1] => 2
[3] => 4
)
array_reduce():
Reduces an array to a single value using a callback function.
$array = [1, 2, 3, 4, 5];
function myFunction($carry, $item) {
return $carry + $item;
}
$result = array_reduce($array, 'myFunction');
echo $result;
Output:
15
PHP Array Functions
array_unique():
Removes duplicate values from an array.
$array = [1, 2, 2, 3, 3, 4];
$uniqueArray = array_unique($array);
print_r($uniqueArray);
Output:
Array
(
[0] => 1
[1] => 2
[3] => 3
[5] => 4
)
array_slice():
Returns a slice of an array.
$array = [1, 2, 3, 4, 5];
$slice = array_slice($array, 2);
print_r($slice);
Output:
Array
(
[0] => 3
[1] => 4
[2] => 5
)
array_chunk():
Splits an array into chunks.
$array = [1, 2, 3, 4, 5];
$chunks = array_chunk($array, 2);
print_r($chunks);
Output:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 3
[1] => 4
)
[2] => Array
(
[0] => 5
)
)
PHP Array Methods for String Manipulation
implode():
Joins array elements with a string.
$array = ['Hello', 'World', '!'];
$string = implode(' ', $array);
echo $string;
Output:
Hello World !
explode():
Splits a string by a string into an array.
$string = 'Hello World !';
$array = explode(' ', $string);
print_r($array);
Output:
Array
(
[0] => Hello
[1] => World
[2] => !
)
ghola hi khde
ReplyDelete