Skip to main content

Arrays In PHP

Have you ever heard about Array.An Array is a PHP datatype used to store a group of data into a variable. Each array item is stored as a key and value pair. Arrays are classified as “indexed array” and “associative array” based on the key specification. The Indexed array has default index starts with ‘0’ and the associative array contains the user-defined key index. The array can also be classified as “one-dimensional array” and “multi-dimensional array” based on the level of the array index. In this tutorial, we are going to see some of the important array functions in PHP.

PHP Array Functions

In this section, we are going to see some of the widely used PHP array functions. I explain the purpose of the array functions, the parameter it requires and the default values of the parameter. This section includes small and simple PHP code snippets to explain the usage of each array function. 
range(start, end)
PHP range() function is used get the array of elements in the range between the start and end parameters. The range can be a numeric or alphabet. The following code shows how to get the array of alphabets that exist between “e” and “i”.
$character_array = range('e','i'); //  returns array('e','f','g','h','i');
reset(array)
The reset() function is used to reset the position of the array pointer. It requires the array variable as its argument to move the array pointer to its original position.
$character_array = range('e','i'); //  returns array('e','f','g','h','i');
print next($character_array); // prints f
print next($character_array); // prints g
reset($character_array);
print current($character_array); // prints the first element 'e' after reset
array_slice(array, offset, length, preserve_keys)
It is used to get the sub-array from the given input array. It accepts the input array, starting position as offset and the length of the sub array. The length parameter is optional. If it is not specified, the length of the input array will be taken by default. The fourth argument is to preserve keys by avoiding the default index reordering behavior.
$color_array = array("Red","Blue","Green","Yellow","Brown"); 
sliced_array = array_slice($color_array,1,3);//  now the sliced array will be array("Blue","Green","Yellow")
shuffle()
This function has the array as an argument to change the position of its value in a random manner.
array_chunk()
array_chunk() is used to break an array into the specified length of chunks, this function is used.
$character_array = array('e','f','g','h','i');
$chunks = array_chunk($character_array,2);
//$chunks will be as follows
/*
Array (
[0] => Array (
[0] => e
[1] => f
)
[1] => Array (
[0] => g
[1] => h
)
[2] => Array (
[0] => i
)
)
*/
merge()
As per the name of this function, it is for joining one or more arrays into a single array. The code shows the example,
$color_array1 = array("Red","Blue","Green");
$color_array2 = array("Yellow","Brown");
// returns array("Red","Blue","Green","Yellow","Brown");
$color_array = array_merge($color_array1,$color_array2);
array_key_exists()
This function is used to check whether the given key exists in the array or not. This function takes a key element and the input array as arguments.
array_keys(), array_values()
These two functions will get the keys and values of an input array, respectively. The following code shows the usage method of these functions and the resultant array of keys and values.
$student_array = array("name"=>"Martin","mark"=>"90","grade"=>"A");
$key_array = array_keys($student_array) // returns array("name","mark","grade")
$value_array = array_values($student_array) // returns array("Martin","90","A")
array_push()/array_pop()/array_shift()/array_unshift()
array_push() is used to push some values into an array. Using this function, a new element will be added to the end of a given input array. The array_pop()is used to get the last element of the array. The array_shift() and the array_unshift() methods are as similar as array_push() array_pop(). But, the insert and retrieve operations are performed at the starting position of an array.
array_splice()
This function is used to remove set of elements from a given array like array_slice. And it can also replace the removed elements with the new set of elements, provided, the length of the removed set and new array both should be same.
$character_array =  array('e','f','g','h','i');
$number_array = array('1','2','3');
$changed_array = array_splice($character_array,1,3,$number_array); // will hold the element like: array('e','1','2','3','i');
extract()/compact()
The compact() is used to convert the variable into an array, whereas, the extract() is used to extract an array and convert the element of that array as variables. The example shows it as follows. The second and third argument of extract() is optional. It is used to add a prefix to the variable name.
$student_array = array("name"=>"Martin","mark"=>"90","grade"=>"A");
extract($student_array,EXTR_PREFIX_ALL,"student"); 
echo $student_name . "-" . $student_mark . "(" . $student_grade . ")"; // prints as Martin - 90(A)
$compact_array = compact("student_name","student_mark","student_

Comments

Popular posts from this blog

Create facebook messenger chatbot using PHP

Chatbots are the latest sensation in social media communication channels. These automated chat systems are especially build to receive vistiors on social media chats and provide basic information to the visitors about your business. This information could include event schedules, product information, latest deals, store offers and general information about the brand. Entrepreneurs and brand marketers employ chatbots to handle the bulk of chats queries. This way, a large number of queries could be easily handled with minimum costs. Chatbots help reduces the dependence on human customer service representatives (CSR). These chatbots vet out common queries so that the human CSR cold focus on queries that require processing of multiple information sources. Since chatbots steer all conversation toward a pre-set direction, it is easy and time-efficient to use these chatbots instead of human CSR. In this article, I will create a simple Facebook chatbot that could carry out an

Build chatbot with node js and react js

User Experience is given a lot of attention while building any application these days. More and more brands are leveraging chatbots to service their customers, market their brand, and even sell their products. There are a lot of awesome tools out there which helps in building an intelligent bot very easily like Google’s DialogFlow, Amazon Lex, etc, most of which implement their own Natural Language Processing (NLP) logic. However, in some cases, we don’t really need an intelligent bot. Whenever we have a small application having a limited set of options to choose from, it’s not really necessary to use NLP based tools like Google’s DialogFlow. You need to integrate with them (which is pretty easy though), and you need to make a network call to get the results. Instead, you would want to define your rules locally in those cases. Here we will build a simple chatbot using React Simple Chatbot library and add it to our pizza-builder app using which we can build ou

Now About PHP Info

Have you ever heard about phpinfo(). Yes, this function is used to know about configuration details of PHP installed in our machine. Such detailed information returned by this phpinfo() includes platform information, PHP and server environment, HTTP header information, PHP core details like version and directives status, License information and etc. phpinfo() function has an optional argument. If this function is called with no argument, then will display all information. We can check it by executing the following code. <? php phpinfo (); ?> We can also request specific details to be displayed to the browser by passing available constants to this function. Following list shows such available options. INFO_GENERAL – This will return information about the platform, compiler, architecture, API and etc. INFO_CREDITS – This option provides a hyperlink which shows details about authors, documentation, QA and infrastructure team. INFO_CONFIGURATION – Disp