Core Function InArray

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "<pre> InArray( <array>, <needle>, <strict>, <flag> ) </pre> === Description === Checks if a value exists in an array === Parameters === ==== array ==== The array to use. ==...")
 
m (1 revision)
 
(6 intermediate revisions by one user not shown)
Line 15: Line 15:
 
==== needle ====
 
==== needle ====
  
The searched value.
+
The searched value (Can be an array).
  
 
Note:
 
Note:
  
If needle is a string, the comparison is done in a case-sensitive manner.
+
If needle is a string (or the value in the needle array), the comparison is done in a case-sensitive manner.
  
 
(Unless the flag is set to ignore case)
 
(Unless the flag is set to ignore case)
Line 26: Line 26:
  
 
Optional; If true the the array value and the needle must be same TYPE such as both Int32s.
 
Optional; If true the the array value and the needle must be same TYPE such as both Int32s.
 +
 +
Default: false
  
 
==== flag ====
 
==== flag ====
Line 43: Line 45:
 
=== Return Value ===
 
=== Return Value ===
  
Success: Returns 1.
+
Success: Returns true.
  
Failure: Returns -1.
+
Failure: Returns false.
  
 
=== Remarks ===
 
=== Remarks ===
Line 54: Line 56:
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
 +
$os = array("Mac", "NT", "Irix", "Linux", 10, array("One", "Two", "Three"));
 +
if (inArray($os, "Irix"))
 +
    echo "Got Irix\n";
 +
else
 +
    echo "Not Got Irix\n";
 +
 +
if (inArray($os, "mac"))
 +
    echo "Got mac\n";
 +
else
 +
    echo "Not Got mac\n";
 +
 +
if (inArray($os, "mac", 0, 1))
 +
    echo "(ignoreCase) Got mac\n";
 +
else
 +
    echo "(ignoreCase) Not Got mac\n";
 +
 +
if (inArray($os, "10"))
 +
    echo "Got 10\n";
 +
else
 +
    echo "Not Got 10\n";
 +
 +
if (inArray($os, "10", 1))
 +
    echo "(strict) Got 10\n";
 +
else
 +
    echo "(strict) Not Got 10\n";
 +
 +
if (inArray($os, "Two"))
 +
    echo "(noRecursion) Got Two\n";
 +
else
 +
    echo "(noRecursion) Not Got Two\n";
 +
 +
if (inArray($os, "Two", 0, 2))
 +
    echo "(Recursion) Got Two\n";
 +
else
 +
    echo "(Recursion) Not Got Two\n";
 +
</syntaxhighlight>
 +
 +
Needle as an array
 +
 +
<syntaxhighlight lang="sputnik">
 +
$os = array("Mac", "NT", "Irix", "Linux", 10, array("One", "Two", "Three"));
 +
if (inArray($os, array("Linux", "Irix")))
 +
    echo "Got Irix or Linux\n";
 +
else
 +
    echo "Not Got Irix nor Linux\n";
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 12:37, 14 June 2015

InArray( <array>, <needle>, <strict>, <flag> )

Contents

Description

Checks if a value exists in an array

Parameters

array

The array to use.

needle

The searched value (Can be an array).

Note:

If needle is a string (or the value in the needle array), the comparison is done in a case-sensitive manner.

(Unless the flag is set to ignore case)

strict

Optional; If true the the array value and the needle must be same TYPE such as both Int32s.

Default: false

flag

Optional; Change how the InArray works

1 = Ignore case on string checks

2 = Use recursion

You can add them together for example:

1 | 2

Default: 0

Return Value

Success: Returns true.

Failure: Returns false.

Remarks

None.

Example

$os = array("Mac", "NT", "Irix", "Linux", 10, array("One", "Two", "Three"));
if (inArray($os, "Irix"))
    echo "Got Irix\n";
else
    echo "Not Got Irix\n";
 
if (inArray($os, "mac"))
    echo "Got mac\n";
else
    echo "Not Got mac\n";
 
if (inArray($os, "mac", 0, 1))
    echo "(ignoreCase) Got mac\n";
else
    echo "(ignoreCase) Not Got mac\n";
 
if (inArray($os, "10"))
    echo "Got 10\n";
else
    echo "Not Got 10\n";
 
if (inArray($os, "10", 1))
    echo "(strict) Got 10\n";
else
    echo "(strict) Not Got 10\n";
 
if (inArray($os, "Two"))
    echo "(noRecursion) Got Two\n";
else
    echo "(noRecursion) Not Got Two\n";
 
if (inArray($os, "Two", 0, 2))
    echo "(Recursion) Got Two\n";
else
    echo "(Recursion) Not Got Two\n";

Needle as an array

$os = array("Mac", "NT", "Irix", "Linux", 10, array("One", "Two", "Three"));
if (inArray($os, array("Linux", "Irix")))
    echo "Got Irix or Linux\n";
else
    echo "Not Got Irix nor Linux\n";
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox