Array Functions in PHP
Array Functions in PHP ,Well there are a whole list of functions in Array.There are a few important Array functions which are widely apply. In this article we will step through which Array functions which commonly used. In the meantime refer the Link below on where to find Array Functions resources
There are many Array Type in PHP
Below List show a few of Important Array functions that we will discuss in this Article.
- Dumping Arrays
- Sorting Arrays
- Counting Arrays
- Shuffling Arrays
- Checking Arrays
- Array Replacement
There are a few more Array Functions that can be useful, but in this article we will not be able to cover every single one of them . But anyway you can try them out yourself by referring the Link which i have provided above. There are very clear and comprehensive explanation provided.
Array Functions in PHP/ Dumping Arrays
var_dump is used when you want to dump all the values or variable contains inside an Array. Normally this is used when you want to empty the Array Container, clearing content in the Array Container, or replacing variables in the Array Container etc.
Refer the Code Below
var_dump($stock);
Output
Array Functions in PHP /Sorting Arrays
There are a few Sorting array Function available, In here we will discuss only one of them as an example
- asort()
- ksort()
- krsort()
- rsort()
When ” asort ” is used, it will maintain the actual element sequence or index order in an Array.
Refer the Code Below
<?php $stock = array("hammer","saw","screwdriver","nail"); $stock['motorised']= "chainsaw"; asort($stock); foreach ($stock as $number => $value){ echo $number.' = '.$value.'<BR>'; } var_dump($stock); ?>
Output
Array Functions in PHP /Counting Arrays
To count the Numbers of arrays inside the array container you use the “count()” function.
Refer the Code Below
<?php $stock = array("hammer","saw","screwdriver","nail"); $stock['motorised']= "chainsaw"; asort($stock); $array_number=count($stock); foreach ($stock as $number => $value){ echo $number.' = '.$value.'<BR>'; } var_dump($stock); echo '<BR>'. $array_number; ?>
Output
Shuffling Array
The Shuffling Array function is being used when you want the sequence of the Array to appear differently when the browser page is being refresh.
Refer the Code Below
<?php $stock = array("hammer","saw","screwdriver","nail"); $stock['motorised']= "chainsaw"; shuffle($stock); $array_number=count($stock); foreach ($stock as $number => $value){ echo $number.' = '.$value.'<BR>'; } var_dump($stock); echo '<BR>'. $array_number; ?>
Output
![shuffle in php](https://www.creatifwerks.com/wp-content/uploads/2017/11/4-1.jpg)
![shuffle in php](https://www.creatifwerks.com/wp-content/uploads/2017/11/5.jpg)
Checking Arrays
This is used when you want to check whether an elements exist inside an array.In order to do this you use “in_array” to check it.
Refer the Code Below
<?php $stock = array("hammer","saw","screwdriver","nail"); $stock['motorised']= "chainsaw"; shuffle($stock); $array_number=count($stock); foreach ($stock as $number => $value){ echo $number.' = '.$value.'<BR>'; } var_dump($stock); echo '<BR>'. $array_number; if (in_array("hammer",$stock)){ echo'<BR>'."Yes Hammer is in Stock"; } else{ echo'<BR>'."the Item is not in stock"; } ?>
Output
Array Replacement
This is used when you want to replace the entire element inside an array, with a group of elements in another array.
Refer the Code Below
<?php $stock = array("hammer","saw","screwdriver","nail"); $stock['motorised']= "chainsaw"; shuffle($stock); $replacement=array("hack screw"); $stock=Array_replace($replacement); $array_number=count($stock); foreach ($stock as $number => $value){ echo $number.' = '.$value.'<BR>'; } var_dump($stock); echo '<BR>'. $array_number; if (in_array("hammer",$stock)){ echo'<BR>'."Yes Hammer is in Stock"; } else{ echo'<BR>'."the Item is not in stock"; } ?>
Output
Array Slicing
array_slice functions will be use to extract a Slice of an array.
array_slice() returns the elements sequence from the array with the specified length set in the Parameter
array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = FALSE ]] ) : array
<?php $stock = array("hammer","saw","screwdriver","nail"); // Slice the Array // will Slive the Output Starting at Index 1 with slice Length equals 2 // Return /* 0 = saw 1 = screwdriver array(2) { [0]=> string(3) "saw" [1]=> string(11) "screwdriver" } */ $output = array_slice($stock , 1, 2); foreach ($output as $number => $value){ echo $number.' = '.$value.'<BR>'; } var_dump($output); ?>
Merging Arrays
array_merge -> The below Sample Code will use Array merge function to merge 2 Arrays into 1
<?php $stock = array("hammer","saw","screwdriver","nail"); $anotherStock = array("nails", "Silicone", "screws"); // Merge the Array $arrayMerge = array_merge($stock, $anotherStock ); foreach ($arrayMerge as $number => $value){ echo $number.' = '.$value.'<BR>'; } /* Output as Below 0 = hammer 1 = saw 2 = screwdriver 3 = nail 4 = nails 5 = Silicone 6 = screws */ ?>
Hopefully now you have an idea on how to use Array Functions in PHP, and where to find resources.
Check out Adding Object inside Array here
Check Out How to access an Array inside an array here.