Using PHP array

PHP array has a wider set of predefined functions that make PHP array one of the most used features of PHP. We will not discuss all the available PHP array functions. We will discuss few of the functions that can be very useful for us in our data structure operations. Here are the PHP array functions:

  • array_pop: This pops the last element of the array similar to stack pop operation. The array is passed as reference to the function. It only takes one argument, that is, the name of the array.
  • array_push: This pushes one or more elements at the end of the array, just like a stack push operation. We have seen that we can push one element at a time using push. In PHP array, we can push multiple values at the end of the current array. The array is passed as a reference in the function as shown:
$countries = []; 
array_push($countries, 'Bangladesh', 'Bhutan');
  • current: Each array has an internal pointer to identify where it is at the moment. Initially, it starts from the first element of the array. The current function returns the current pointer of the array and returns the value of the element in the current position. If we consider the array to be a list, these internal pointer functionalities will be required.
  • prev: The prev function moves the internal pointer one step backward. The PHP array can work as a doubly linked list, and prev is used to go the previous pointer.
  • next: The next function moves the internal pointer to the next element.
  • end: The end function moves the internal array pointer to the end of the array.
  • reset: The reset function moves the internal array to the beginning of the array.
  • array_search: This is a very useful function for searching an element in the array. If the element is found in the array, it returns the corresponding index where it was found. If nothing is found, it will return false. If multiple elements are there with the same search key, it will return the first occurrence index. We have to be careful as this function might also return 0 if the element is found in the first index. So, we have to check the boolean false with strict type checking during comparison. The array_search function takes two mandatory arguments, needle, and haystack. Needle is the element we are looking for, and haystack is the array where we are looking for the element. For example, if we are looking for a word in a dictionary, then we can consider the search word such as "needle" and "dictionary" as the haystack. There is an optional third parameter that enables strict type checking for the element. So, if it is set true, it searches the element not only by value, but also by type:
$countries = ["Bangladesh", "Nepal", "Bhutan"]; 

$key = array_search("Bangladesh", $countries);
if ($key !== FALSE)
echo "Found in: " . $key;
else
echo "Not found";

  This will produce the following output:

    Found in: 0

  If we had != inside the if condition check, then it would have shown Not  found in the result.

  • array_sum: This is another handy PHP built-in function to get the sum of a given array. It will return a single numeric value, which is the sum of all elements in the array. It can be an integer or float.
  • array_map: This is a very useful function if we want to change the elements of the array with a certain type of properties. For example, we want to make all text of the array to be in upper case or lower case. Instead of running a loop, we can use this function to do that. The array_map function takes two arguments. The first one is the callable function, and the second one is the array itself. The function returns the modified array, as shown here:
$countries = ["bangladesh", "nepal", "bhutan"]; 
$newCountries = array_map(function($country) {
return strtoupper($country);
}, $countries);

foreach ($newCountries as $country)
echo $country . " ";

   Alternatively, we can write it simply like this:

$countries = ["bangladesh", "nepal", "bhutan"]; 
$newCountries = array_map('strtoupper', $countries);

foreach ($newCountries as $country)
echo $country . " ";

The preceding code applies an array_map function to capitalize each word in a given array. Both codes will produce the following output:

BANGLADESH
NEPAL
BHUTAN
  • array_rand: If we need to pick one or more items randomly from a given array, this function can be very useful. The default value is 1 for the number of items to return, but we can always increase it.
  • array_shift: This function shifts an element from the beginning of the array, which is very much similar to our dequeue operation in a queue data structure. The removed element is returned from the function:
$countries = ["bangladesh", "nepal", "bhutan"]; 
$top = array_shift($countries);
echo $top;

This will show the output bangladesh in the command line. The $countries array will have only nepal and bhutan in it.

  • array_unshift: This function adds one or more items at the beginning of the array and unshift existing items.
  • shuffle: If we need to shuffle an array for any reason, we can use this function. This function can be very handy to randomize the whole array.
  • array_intersect: This function takes two or more arrays as arguments and returns the common items from the first array and finds out the existence in other arrays. This function also preserves the keys.
  • array_diff: This function calculates the difference between an array and other given arrays. Like the array_intersect function, this function also takes multiple arrays as arguments, where the first argument is the base array and, others are compared for differentiating with it.

There are many useful array functions in PHP, and they are solving many existing data structure and algorithm problems. We can find a list of built-in array functions in PHP documentation. For the purpose of this book, we will explore a few more array functions for sorting in the upcoming sections. For other functions, PHP .NET is recommended for further reading.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset