Core Function Search

From Sputnik Wiki
Jump to: navigation, search
Search( <array>, <value>, <flag>, <returnKeys> )

Contents

Description

Returns a new array consisting of the elements of the input array that match the given value.

Parameters

array

The array to use.

value

A value to search for.

OR

An array of values to search for.

flag

Optional; If the third parameter strict is set to TRUE then the Search() function will search for identical elements in the array.

This means it will also check the types of the values in the array, and objects must be the same type as well.

Default is 0.

invert_flag

Optional; If the invert_flag is higher than 0 the search will be inverted and everything that does not match will be returned instead of everything that does match.

Default is 0.

returnKeys

Optional; If the returnKeys is higher than 0 the search will return an array of KEYS instead of an array of VALUES.

Default is 0.

Return Value

Success - Returns new array with information.

Failure - Returns empty array.

Remarks

This also includes Hash values with the array values.

Example

Return everything that matches

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red', 4 => 10);
 
$key = Search($array, 'green');
println("Search for 'green' below");
foreach($key as $k => $v)
{
	println("Key '$k' Value '$v'");
}
println("Search for 'green' above");
 
println("");
$key = Search($array, 'red');
println("Search for 'red' below");
foreach($key as $k => $v)
{
	println("Key '$k' Value '$v'");
}
println("Search for 'red' above");
 
println("");
$key = Search($array, '10', 1);
println("Search for '10' below");
foreach($key as $k => $v)
{
	println("Key '$k' Value '$v'");
}
println("Search for '10' above");
 
println("");
$key = Search($array, 10, 1);
println("Search for 10 below");
foreach($key as $k => $v)
{
	println("Key '$k' Value '$v'");
}
println("Search for 10 above");

For a match from an array of values

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red', 4 => 10);
 
$key = Search($array, array('green','blue'));
println("Search for 'green' below");
foreach($key as $k => $v)
{
	println("Key '$k' Value '$v'");
}

Example of invert_flag use

$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red', 4 => 10);
 
$key = Search($array, 'green', 0, 1);
println("Search for NOT 'green' below");
foreach($key as $k => $v)
{
	println("Key '$k' Value '$v'");
}
println("Search for NOT 'green' above");
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox