PHP

Some Important And Useful Array Function In PHP.

An array play an important role in web developemnt. Inbulit array function makes web development very easy. PHP has many inbuilt array function to make web develoment easy. In this post we will learn some important and useful arra function in PHP. Using array function we can access and manipulate arrays.

sizeof($arr) - > This function is used to find the sizeof of an given array
Example:
$language = array('PHP','Java','Kotlin','Scala','C','C++');
echo "Size of the array is: ". sizeof($language); 


is_array($arr) - > This function is used to determine the given variable is an array or not.
Example:
$language = array('PHP','Java','Kotlin','Scala','C','C++');
echo is_array($language) ? 'Array' : 'not an Array';

in_array($var, $arr) - > This function is used to check whether the variable is exist in an array or not.
$language = array('PHP','Java','Kotlin','Scala','C','C++');
echo in_array('JavaScript', $lamborghinis) ? 'Value exist' : 'Value not exist';

array_merge($arr1, $arr2)-> This function is used to merge two different array into single array.
$language = array('PHP','Java','Kotlin','Scala','C','C++');
$database = array('MYSQL','POSTGRESQL','ORACLE','DB2','MONGODB');
$merged_array = array_merge($language,$database);
print_r($language,$database);

array_values($arr)

In an array , data is stored in form of key-value pairs, whether key can be numerical(incase of index array) or user defined strings (in case of associative array) and values.

If we want to get value from an array and want to store them in an another array , we can use array_values() function.

$language = array('PHP','Java','Kotlin','Scala','C','C++');
$database = array('MYSQL','POSTGRESQL','ORACLE','DB2','MONGODB');
$merged = array_values($merged);
print_r($merged);

array_keys($arr)

Just like values, we can also extract just the keys from an array. this function is used to extract the keys from the array.

$language = array('PHP','Java','Kotlin','Scala','C','C++');
$database = array('MYSQL','POSTGRESQL','ORACLE','DB2','MONGODB');
$merged_key = array_values($merged);
print_r($merged);

array_pop($arr)
This function is used to removes the last element of the array.
$language = array('PHP','Java','Kotlin','Scala','C','C++');
array_pop($language);
print_r($language);

array_push($arr, $val)

This function is used to add a new element at the end of the array.
	$language = array('PHP','Java','Kotlin','Scala','C','C++');
	array_push($language, "JavaScript");
	
	
array_shift($arr)
This function is used to removes the first element of the array	.
$language = array('PHP','Java','Kotlin','Scala','C','C++');
array_shift($language);
print_r($language); 

Related Articles

Leave a Reply

Back to top button