PHP Array Functions
PHP Arrays
An array stores multiple values in one single variable at a time, in normal variable value overwritten as soon as you store new value.
$city = array("New York", "Atlanta", "Manchester"); echo "I have been to " . $city[0] . ", " . $city[1] . " and " . $city[2] . ".";
Length of an Array, count function
Most of the time you need to know the count or total array members
$city = array("New York", "Atlanta", "Manchester"); echo count($city);
Adding new values to an array, array_push()
Values can be added in an array at the time of initialization, just like above example 1, or later as well.
$city = array(); \\only array declaration, not initialization $city = array("New York", "Atlanta", "Manchester"); \\array declaration, and initialization in single line array_push($city,"Paris"); \\ add one value array_push($city,"Madrid", "Dublin", "Sydney"); \\ add multiple values
Remove Duplicate values from an array
The array_unique() function removes duplicate values from an array. If two or more array values are the same, the first member will be kept and the other will be removed.
$city = array("New York", "Atlanta", "Manchester", "Madrid", "Dublin", "Manchester", "Sydney"); $city = array_unique($city);
it will keep 3rd Manchester, removes 6th Manchester and moves Sydney at 6th position
PHP get random values from an array, array_rand()
The array_rand() function returns a random key from an array
$city = array("New York", "Atlanta", "Manchester", "Madrid", "Dublin", "Sydney"); $rand_val=array_rand($city,3); \\3 Specifies how many random keys to return echo $a[$rand_val[0]]." "; echo $a[$rand_val[1]]." "; echo $a[$rand_val[2]];
Sorting Array, sort()
The elements in an array can be sorted. It can be sorted in alphabetical or numerical order, descending or ascending.
$city = array("New York", "Atlanta", "Manchester", "Madrid", "Dublin", "Sydney"); sort($city);
sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the value
krsort() - sort associative arrays in descending order, according to the key
shuffling array values, shuffle()
shuffle() function Randomizes the order of the elements in the array
$city = array("New York", "Atlanta", "Manchester", "Madrid", "Dublin", "Sydney"); shuffle($city);
Associative Arrays
Associative arrays are same as normal arrays, only difference is you can assign names to the key, you can get value by giving the name associated to them.
How you create an associative array: $Highest_Mountain = array("Mount Everest"=>"8848", "K2"=>"8611", "Kangchenjunga"=>"8586"); or: $Highest_Mountain['Mount Everest'] = "8848"; $Highest_Mountain['K2'] = "8611"; $Highest_Mountain['Kangchenjunga'] = "8586";
Get Max or min values
You can get maximum or minimum array values using max or min function
$Highest_Mountain = array("Mount Everest"=>"8848", "K2"=>"8611", "Kangchenjunga"=>"8586"); $max_value = max($Highest_Mountain); $min_value = min($Highest_Mountain);
Convert Form Posted values as string
Whenever a HTML posts input values as array as mentioned in 1st and 2nd line of below code snippet
$StrARR = ''; foreach ($_POST['EMPID'] as $names) {$StrARR .= $names.',';} \\remove trailing , from value to use in other function $StrARR = substr($StrARR, 0, -1);
in_array()
Check if any value in an array
$city = array("New York", "Atlanta", "Manchester", "Madrid", "Dublin", "Sydney"); if (in_array("Dublin", $city, true)) {echo " Yes";}
array_slice()
Get first few elements from an array, array_slice returns a slice of an array
$city = array("New York", "Atlanta", "Manchester", "Madrid", "Dublin", "Sydney"); array_slice($city, 0, 2) // return the first two elements
explode ()
Explode string and convert into array
$city = array("New York", "Atlanta", "Manchester", "Madrid", "Dublin", "Sydney"); $city = explode("," , $city); explode(separator,string,limit) \\limit Specifies the number of array elements to return.
implode()
The implode() function returns a string from the elements of an array.
$seasons = array("Autumn", "Winter", "Spring", "Summer"); echo $new_textline = implode(" * ", $seasons);
HTML Select in Form
$SARRAY = ARRAY(); \\from db or from post or get values Select Category $resultCat = mysqli_query($conn, "SELECT * FROM prodcat ORDER BY HEADING"); while($row = mysqli_fetch_array($resultCat)) { $CATEGORY_ID = $row['ID']; $CATEGORY_NAME = $row['HEADING']; echo "$CATEGORY_NAME"; }