Course Content
State Management
0/1
Regular Expressions?
0/1
About Lesson

Different Types of Array?

array_unshift() :

is a PHP function that adds one or more elements to the beginning of an array. It effectively prepends elements to the front of the array, pushing the existing elements towards higher numeric keys.

Here is the syntax for array_unshift():

array_unshift(array &$array, mixed $value1 [, mixed $value2 [, mixed $… ]]): int

$array: The array to which elements will be prepended. This parameter is passed by reference, which means the original array is modified.

$value1, $value2, and so on: The values to be added to the beginning of the array.

The function returns the new number of elements in the array after the operation.

Here’s a simple example of how to use array_unshift():

PHP
<?php

$colors = array("green", "blue");

// Add elements to the beginning of the array

array_unshift($colors, "red", "yellow");

print_r($colors);

?>

Output:

Array

(

    [0] => red

    [1] => yellow

    [2] => green

    [3] => blue

)

In this example, “red” and “yellow” are added to the beginning of the $colors array using array_unshift(). As a result, “green” and “blue” move to higher numeric keys, maintaining their relative order within the array.

array_unshift() is particularly useful when you need to add elements to the beginning of an array without affecting the existing keys. However, if you want to add elements to the end of an array, you would use array_push().

array_push() :

is a PHP function used to add one or more elements to the end of an array. It’s a convenient way to append elements to an existing array.

array_pop():

is a PHP function used to remove the last element from an array and return its value. It modifies the original array by removing the last element.

Here’s the basic syntax for array_pop():

array_pop(array &$array): mixed

$array: The array from which the last element will be removed. This parameter is passed by reference, which means the original array is modified.

The function returns the value of the last element that was removed from the array. If the array is empty, array_pop() returns null.

Here’s an example of how to use array_pop):

PHP
<?php

$stack = array("orange", "banana", "apple", "raspberry");

// Remove the last element from the array

$lastFruit = array_pop($stack);

echo "Removed fruit: " . $lastFruit . "\n";

print_r($stack);

?>

Output:

Removed fruit: raspberry

Array

(

    [0] => orange

    [1] => banana

    [2] => apple

)

In this example, array_pop() removes the last element (“raspberry”) from the $stack array and returns its value. After the operation, the array contains only the remaining elements (“orange”, “banana”, “apple”).

array_pop() is commonly used when you want to remove and process the last element of an array, such as when implementing a stack data structure. If you need to remove elements from the beginning of an array, you would typically use array_shift().

array_shift():

is a PHP function used to remove the first element from an array and return its value. It modifies the original array by removing the first element and shifting all other elements down to lower numeric keys.

The function returns the value of the first element that was removed from the array. If the array is empty, array_shift() returns null.

array_keys() :

is a PHP function used to retrieve all the keys or a subset of keys from an array. It returns an array containing the keys of the input array.

Here’s the basic syntax for array_keys():

array_keys(array $array, mixed $search_value = null, bool $strict = false): array

$array: The input array from which to extract keys.

$search_value (optional): If specified, only keys with this value will be returned.

$strict (optional): If set to true, the function performs a strict type comparison when checking for the search value.

The function returns an array containing the keys of the input array. If $search_value is provided, only the keys with that value will be returned. If no keys are found matching the search value, an empty array is returned.

Here are some examples of how to use array_keys():

Getting all keys from an array:

PHP
<?php

$array = array("a" => "apple", "b" => "banana", "c" => "cherry");

$keys = array_keys($array);

print_r($keys);

?>

Output:

Array

(

    [0] => a

    [1] => b

    [2] => c

)

Getting keys with a specific value:

<?php

$array = array("a" => "apple", "b" => "banana", "c" => "apple");

$keys = array_keys($array, "apple");

print_r($keys);

?>

Output:

Array

(

    [0] => a

    [1] => c

)

array_keys() is useful for extracting keys from an array, especially when you need to work with keys separately from the corresponding values. It’s commonly used in PHP applications for various purposes, such as data processing, validation, and array manipulation.

array_key_exists():

is a PHP function used to check if a specific key exists in an array. It returns true if the key exists in the array, and false otherwise.

Here’s the basic syntax for array_key_exists():

array_key_exists(mixed $key, array $array): bool

$key: The key you want to check for existence in the array.

$array: The array in which you want to check for the key’s existence.

The function returns true if the key exists in the array, and false otherwise. It performs a case-sensitive check for the key in the array.

Here’s an example of how to use array_key_exists():

PHP
<?php

$colors = array("red" => "#ff0000", "green" => "#00ff00", "blue" => "#0000ff");

if (array_key_exists("green", $colors)) {

    echo "The key 'green' exists in the array.";

} else {

    echo "The key 'green' does not exist in the array.";

}

?>

Output:

The key 'green' exists in the array.

In this example, array_key_exists() checks if the key “green” exists in the $colors array. Since the key “green” does exist, the function returns true, and the corresponding message is echoed.

array_key_exists() is commonly used to avoid undefined index errors when accessing array elements, especially when dealing with user input or dynamically generated data. It’s a useful function for checking the presence of keys in associative arrays before attempting to access their corresponding values.

in_array():

is a PHP function used to check if a specific value exists in an array. It returns true if the value is found in the array, and false otherwise.

Here’s the basic syntax for in_array():

in_array(mixed $needle, array $haystack, bool $strict = false): bool

$needle: The value you want to search for in the array.

$haystack: The array in which you want to search for the value.

$strict (optional): If set to true, the function performs a strict type comparison when checking for the value.

The function returns true if the value ($needle) is found in the array ($haystack), and false otherwise. It performs a case-sensitive search by default.

Here’s an example of how to use in_array():

PHP
<?php

$fruits = array("apple", "banana", "cherry");

if (in_array("banana", $fruits)) {

    echo "Found banana in the array.";

} else {

    echo "Did not find banana in the array.";

}

?>

Output:

Found banana in the array.

array_reverse():

is a PHP function used to reverse the order of elements in an array. It creates a new array with the elements of the original array in reverse order, leaving the original array unchanged.

Here’s the basic syntax for array_reverse():

array_reverse(array $array, bool $preserve_keys = false): array

$array: The array whose elements you want to reverse.

$preserve_keys (optional): If set to true, the function preserves the original keys of the array. If set to false (default), the function reassigns numeric keys in reverse order.

The function returns a new array containing the elements of the original array in reverse order.

Here’s an example of how to use array_reverse():

PHP
<?php

$numbers = array(1, 2, 3, 4, 5);

$reversedNumbers = array_reverse($numbers);

print_r($reversedNumbers);

?>

Output:

Array

(

    [0] => 5

    [1] => 4

    [2] => 3

    [3] => 2

    [4] => 1

)

In this example, array_reverse() reverses the order of elements in the $numbers array and returns a new array $reversedNumbers with the reversed elements.

array_reverse() is useful when you need to reverse the order of elements in an array for various purposes, such as displaying data in a different order or processing data in reverse order.

array_merge() is a PHP function used to merge one or more arrays into a single array. It takes multiple arrays as arguments and returns a new array containing the elements of all the input arrays.

Here’s the basic syntax for array_merge():

array_merge(array $array1, array $array2 [, array $… ]): array

$array1, $array2, and so on: The arrays to be merged.

The function returns a new array containing the elements of all the input arrays. If two or more arrays have the same string keys, the later value will overwrite the previous one.

Here’s an example of how to use array_merge():

PHP
<?php

$array1 = array("a" => "apple", "b" => "banana");

$array2 = array("c" => "cherry", "d" => "date");

$mergedArray = array_merge($array1, $array2);

print_r($mergedArray);

?>

Output:

Array

(

    [a] => apple

    [b] => banana

    [c] => cherry

    [d] => date

)

In this example, array_merge() merges the elements of $array1 and $array2 into a new array $mergedArray.

If there are numeric keys in the input arrays, array_merge() re-indexes the resulting array starting from 0 and assigns new numeric keys to the elements.

array_merge():

is commonly used when you need to combine multiple arrays into one, such as when working with data from different sources or when constructing complex data structures from smaller components. It’s a versatile function for array manipulation in PHP applications.

The spread operator (…) is a feature introduced in PHP 7.4. It allows you to unpack elements from an array or traversable object into individual elements or arguments.

The spread operator is particularly useful in situations where you want to merge arrays, pass multiple arguments to a function, or define an array with existing elements plus additional ones.

Here are some common uses of the spread operator:

1. Merging Arrays

PHP
<?php

$array1 = [1, 2, 3];

$array2 = [4, 5, 6];

$mergedArray = [...$array1, ...$array2];

print_r($mergedArray);

?>

Output:

Array

(

    [0] => 1

    [1] => 2

    [2] => 3

    [3] => 4

    [4] => 5

    [5] => 6

)

2. Function Arguments

PHP
<?php

function sum(...$numbers) {

    return array_sum($numbers);

}

echo sum(1, 2, 3, 4, 5); // Outputs: 15

?>

3. Array Creation

PHP
<?php

$array1 = [1, 2, 3];

$array2 = [...$array1, 4, 5, 6];

print_r($array2);

?>

Output:

Array

(

    [0] => 1

    [1] => 2

    [2] => 3

    [3] => 4

    [4] => 5

    [5] => 6

)

4. Unpacking Nested Arrays

PHP
<?php

$nestedArray = [[1, 2], [3, 4]];

$flattenedArray = [...$nestedArray[0], ...$nestedArray[1]];

print_r($flattenedArray);

?>

Output:

Array

(

    [0] => 1

    [1] => 2

    [2] => 3

    [3] => 4

)

The spread operator simplifies and enhances the readability of code, especially when dealing with arrays and variadic functions. It’s a powerful feature that allows for concise and expressive PHP programming.

list() is a language construct used to assign values to multiple variables in one operation. It’s often used in conjunction with array functions or operations that return multiple values to easily extract those values into separate variables.

Here’s the basic syntax of list():

list($var1, $var2, …) = $array;

$var1, $var2, etc.: Variables to which the values from the array will be assigned.

$array: The array containing the values to be assigned to the variables.

The number of variables must match the number of elements in the array, otherwise, PHP will issue a warning.

Here’s a simple example of how to use list():

PHP
<?php

$person = array("John", "Doe", 30);

list($firstName, $lastName, $age) = $person;

echo "First Name: $firstName, Last Name: $lastName, Age: $age";

?>

Output:

First Name: John, Last Name: Doe, Age: 30 In this example, list() assigns the values from the $person array to the variables $firstName, $lastName, and $age respectively