Difference between revisions of "JArrayHelper/getValue"
From Joomla! Documentation
< API15:JArrayHelper
(New page: ===Description===
Utility function to return a value from a named array or a specified default
<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>[[Description:JArray...) |
m (removing red link to edit, no existant pages) |
||
(One intermediate revision by one other user not shown) | |||
Line 3: | Line 3: | ||
<span class="editsection" style="font-size:76%;"> | <span class="editsection" style="font-size:76%;"> | ||
− | <nowiki>[< | + | <nowiki>[<! removed edit link to red link >]</nowiki> |
</span> | </span> | ||
− | + | <! removed transcluded page call, red link never existed > | |
===Syntax=== | ===Syntax=== | ||
Line 17: | Line 17: | ||
!Description | !Description | ||
|- | |- | ||
− | | | + | | &$array |
| | | | ||
| $array A named array | | $array A named array | ||
Line 104: | Line 104: | ||
<span class="editsection" style="font-size:76%;"> | <span class="editsection" style="font-size:76%;"> | ||
− | <nowiki>[< | + | <nowiki>[<! removed edit link to red link >]</nowiki> |
</span> | </span> | ||
− | + | <! removed transcluded page call, red link never existed > | |
===Examples=== | ===Examples=== | ||
Line 119: | Line 119: | ||
format= ,,, | format= ,,, | ||
</dpl> | </dpl> | ||
+ | [[Category:Archived pages API15]] |
Revision as of 08:43, 12 May 2013
The "API15" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.
Description[edit]
Utility function to return a value from a named array or a specified default
[<! removed edit link to red link >]
<! removed transcluded page call, red link never existed >
Syntax[edit]
getValue(&$array, $name, $default=null, $type='')
Parameter Name | Default Value | Description |
---|---|---|
&$array | $array A named array | |
$name | $name The key to search for | |
$default | null | $default The default value to give if no key found |
$type | $type Return type for the variable (INT, FLOAT, STRING, WORD, BOOLEAN, ARRAY) |
Returns[edit]
mixed The value from the source array
Defined in[edit]
libraries/joomla/utilities/arrayhelper.php
Importing[edit]
jimport( 'joomla.utilities.arrayhelper' );
Source Body[edit]
function getValue(&$array, $name, $default=null, $type='')
{
// Initialize variables
$result = null;
if (isset ($array[$name])) {
$result = $array[$name];
}
// Handle the default case
if (is_null($result)) {
$result = $default;
}
// Handle the type constraint
switch (strtoupper($type))
{
case 'INT' :
case 'INTEGER' :
// Only use the first integer value
@ preg_match('/-?[0-9]+/', $result, $matches);
$result = @ (int) $matches[0];
break;
case 'FLOAT' :
case 'DOUBLE' :
// Only use the first floating point value
@ preg_match('/-?[0-9]+(\.[0-9]+)?/', $result, $matches);
$result = @ (float) $matches[0];
break;
case 'BOOL' :
case 'BOOLEAN' :
$result = (bool) $result;
break;
case 'ARRAY' :
if (!is_array($result)) {
$result = array ($result);
}
break;
case 'STRING' :
$result = (string) $result;
break;
case 'WORD' :
$result = (string) preg_replace( '#\W#', '', $result );
break;
case 'NONE' :
default :
// No casting necessary
break;
}
return $result;
}
[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >
Examples[edit]
<CodeExamplesForm />