Category Archives: Php array functions - Page 2

in_array(), php array function to find the match in an array

The in_array() function searches an array for a specific value.

This function returns TRUE if the value is found in the array, or FALSE otherwise.

 

Example

<?php $anyArray = array("Patty","Susan","Amy","Clara"); 
$find = "Heather"; $result = in_array($find, $anyArray); 
if ($result == true){
 echo "$find is in the array"; 
} else {
 echo "$find is NOT in the array"; } ?>

Output

Heather is NOT in the array

array_unique — Removes duplicate values from an array

array_unique — Removes duplicate values from an array
Description

array array_unique ( array array )

array_unique() takes input array and returns a new array without duplicate values.

array_unique() sorts the values treated as string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept.[indent]Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.

 

Example

	<?php
	$input = array(4, "4", "3", 4, 3, "3");
	$result = array_unique($input);
	var_dump($result);
	?>

Output

array(2) {
[0] => int(4)
[2] => string(1) "3"
}
<?php
$array = Array('A','B','C','D','B');
print_r($array); // Array ( [0] => A [1] => B [2] => C [3] => D [4] => B )
$result = array_unique($array);
print_r($result); // Array ( [0] => A [1] => B [2] => C [3] => D ) 
?>

array_sum — php array function to find the sum of the element of the array

The array_sum() array function in PHP will calculate the sum of all values within an array.

Example of array_sum()

<?php
$val1 = array(10, 5, 100);
$val2 = array(10.5, -5.5, 100);
echo "sum(val1) = ". array_sum($val1) . "<br />";
echo "sum(val2) = ". array_sum($val2) . "<br />";
?>

Output of the example

sum(val1) = 115

sum(val2) = 105

array_search() — php array function to search the key of the item in an array

The php array_search function search the given item in an array and returns its corresponding index.It is a good way to check to see if a value exists in an array. Remember that all arrays have a default index of 0, so the first element has a key of 0 when key labels are not specified in the array.
Syntax:

    array_search(value,array,strict)

In the above syntax “value” specifies the value to be searched for, “array” specifies the array in which to search, “strict” specifies a strict comparison (===).

Example:

    <?php
    $b=array("c"=>"Cherry","b"=>"Strawberry");
    $a=array("a"=>"5","b"=>5,"c"=>"5");
    print_r(array_search("Cherry",$b));
    print_r(array_search(5,$a,true);
    ?>

Result:

    c
    b

In the above example the array “$b”, is searched for and displays the value “cherry”, for the array “$a” a strict comparison is done and the result is displayed as “b”,since 5 and “5” are not equal when (===) is used.

array_rand() — php array function to select the one or more array items at random

The array_rand() array function in php will select one or more array items from the array at random. The array_rand() array function accepts one or two arguments . First is the array and second one is the number of items to display from an array . By default the array function displays one item from an array.

Syntax:

  array_rand(array);  // Displays 1 item from array
or array_rand(array, no_of_items); // displays no_of_items from an array

Example:

<?php
$targetArray = array("Sara","Cindy","Julie","Megan");
$rand = array_rand($targetArray, 2);
foreach ($rand as $key => $value) {
echo "$key - <strong>" . $targetArray[$value] . "</strong><br />";
}
?>

Output:

0 - Julie
1 - Megan