Searching...
Saturday 6 July 2013

Locating array elements


The ability to efficiently sift through data is absolutely crucial in today’s information-driven society. This article introduces several functions that enable you to search arrays(what is an array?) in order to locate items of interest.

Searching an Array

The in_array() function searches an array for a specific value, returning TRUE if the value is found and FALSE otherwise. Its prototype follows:

boolean in_array(mixed needle, array haystack [, boolean strict])

Note:- If the needle is a string, then, comparison is  case sensitive.

In the following example, a message is output if a specified state (Ohio) is found in an array consisting of states having statewide smoking bans:

$state = "Ohio";
$states = array("California", "Hawaii", "Ohio", "New York");
if(in_array($state, $states)) echo "Not to worry, $state is smoke-free!";

The optional third parameter, strict, forces in_array() to also consider type.

<?php
$a = array('1.10', 12.4, 1.13);
if (in_array('12.4', $a, true)) {
    echo "'12.4' found with strict check\n";
}
if (in_array(1.13, $a, true)) {
    echo "1.13 found with strict check\n";
}
?>

The above example will output:

1.13 found with strict check

Searching Associative Array Keys

The function array_key_exists() returns TRUE if a specified key is found in an array and FALSE otherwise. Its prototype follows:

boolean array_key_exists(mixed key, array array)

The following example will search an array’s keys for Ohio, and if found, will output information about its entrance into the Union:

$state["Delaware"] = "December 7, 1787";
$state["Pennsylvania"] = "December 12, 1787";
$state["Ohio"] = "March 1, 1803";
if (array_key_exists("Ohio", $state))
printf("Ohio joined the Union on %s", $state["Ohio"]);

The following is the result:

Ohio joined the Union on March 1, 1803

Searching Associative Array Values

The array_search() function searches an array for a specified value, returning its key if located and FALSE otherwise. Its prototype follows:

mixed array_search(mixed needle, array haystack [, boolean strict])

The following example searches $state for a particular date (December 7), returning information about the corresponding state if located:

$state["Ohio"] = "March 1";
$state["Delaware"] = "December 7";
$state["Pennsylvania"] = "December 12";
$founded = array_search("December 7", $state);
if ($founded) printf("%s was founded on %s.", $founded, $state[$founded]);

The output follows:

Delaware was founded on December 7.

Note:- 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

Retrieving Array Keys

The array_keys() function returns an array consisting of all keys located in an array. Its prototype follows:

array array_keys(array array [, mixed search_value [, boolean preserve_keys]])

If the optional search_value parameter is included, only keys matching that value will be returned.
The following example outputs all of the key values found in the $state array:

$state["Delaware"] = "December 7, 1787";
$state["Pennsylvania"] = "December 12, 1787";
$state["New Jersey"] = "December 18, 1787";
$keys = array_keys($state);
print_r($keys);

The output follows:

Array ( [0] => Delaware [1] => Pennsylvania [2] => New Jersey )

Setting the optional preserve_keys parameter (introduced in PHP 5.0.2) to true will cause the array values’ keys to be preserved in the returned array.

Retrieving Array Values

The array_values() functions returns all values located in an array, automatically providing numeric indexes for the returned array. Its prototype follows:

array array_values(array array)

The following example will retrieve the population numbers for all of the states found in $population:

$population = array("Ohio" => "11,421,267", "Iowa" => "2,936,760");
print_r(array_values($population));

This example will output the following:

Array ( [0] => 11,421,267 [1] => 2,936,760 )

Now take another example:-

<?php
$array = array("size" => "XL", "color" => "gold","x" => " ","y" => "gold","z" => "");
print_r(array_values($array));
?>

Above example will produce the following result:

Array ( [0] => XL [1] => gold [2] => [3] => gold [4] => )
 


0 comments:

Post a Comment

 
Back to top!