Skip to main content


Showing posts from September, 2013


1.       ARRAY_SEARCH array_search — Searches the array for a given value and returns the corresponding key if successful Description mixed array_search ( mixed $needle, array $haystack [, bool $strict] ) Searches haystack for needle and returns the key if it is found in the array, FALSE otherwise. Note: If needle is a string, the comparison is done in a case-sensitive manner. If the optional third parameter strict is set to TRUE then the array_search() will also check the types of the needle in the haystack . If needle is found in haystack more than once, the first matching key is returned. To return the keys for all matching values, use array_keys() with the optional search_value parameter instead. Example. array_search() example 'blue', 1 => 'red', 2 => 'green', 3 => 'Red'); $key = array_search('green', $array); // $key = 2; echo $key; $key = array_search('Red', $array, true); // $key = 3;


ARRAY_KEY_EXISTS array_key_exists — Checks if the given key or index exists in the array Description bool array_key_exists ( mixed $key, array $search ) array_key_exists() returns TRUE if the given key is set in the array. key can be any value possible for an array index. array_key_exists() also works on objects. Parameters key             Value to check. search             An array with keys to check. Return Values Returns TRUE on success or FALSE on failure. Examples array_key_exists() example <?php $search_array = array('first' => 1, 'second' => 4); if (array_key_exists('first', $search_array)) { echo "The 'first' element is in the array"; } ?> Example   array_key_exists() vs isset() isset() does not return TRUE for array keys that correspond to a NULL value, while array_key_exists() does. null, 'second' => 4); // returns false isset($search_array['first&#


ARRAY_KEYS array_keys — Return all the keys of an array Description array array_keys ( array $input [, mixed $search_value [, bool $strict]] ) array_keys() returns the keys, numeric and string, from the input array. If the optional search_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the input are returned. As of PHP 5, you can use strict parameter for comparison including type (===). Parameters input An array containing keys to return. search_value If specified, then only keys containing these values are returned. strict As of PHP 5, this parameter determines if strict comparision (===) should be used during the search. Return Values Returns an array of all the keys in input . Examples Example . array_keys() example <?php $array = array ( 0 = > 100 , "color" = > "red" ) ; $a = array_keys ( $array ) ; foreach ( $a as $a1 ) { echo "&


ARRAY_COUNT_VALUES array_count_values — Counts all the values of an array Description array array_count_values ( array $input ) array_count_values() returns an array using the values of the input array as keys and their frequency in input as values. Parameters input The array of values to count Return Values Returns an assosiative array of values from input as keys and their count as value. Errors/Exceptions Throws E_WARNING for every element which is not string or integer . Examples array_count_values() example <?php $array = array ( 1 , "hello" , 1 , "world" , "hello" ) ; print_r ( array_count_values ( $array ) ) ; ?> Out Put Array ( [ 1 ] => 2 [hello] => 2 [world] => 1 ) <?php $array = array ( 1 , "hello" , 1 , "world" , "hello" ) ; $a = array_count_values ( $array ) ; foreach ( $a as $k = > $v ) { echo "key