JArrayHelper/getValue: Difference between revisions
From Joomla! Documentation
m removing red link to edit, no existant pages |
m preparing for archive only |
||
| (One intermediate revision by the same user not shown) | |||
| Line 2: | Line 2: | ||
Utility function to return a value from a named array or a specified default | Utility function to return a value from a named array or a specified default | ||
<! removed transcluded page call, red link never existed > | <! removed transcluded page call, red link never existed > | ||
| Line 103: | Line 101: | ||
</source> | </source> | ||
<! removed transcluded page call, red link never existed > | <! removed transcluded page call, red link never existed > | ||
===Examples=== | ===Examples=== | ||
=== Code Examples === | |||
<dpl> | <dpl> | ||
noresultsheader=\n | noresultsheader=\n | ||
category=getValue | category=getValue | ||
category=JArrayHelper | category=JArrayHelper | ||
namespace=CodeExample | |||
category=MethodExample | category=MethodExample | ||
include=* | include=* | ||
Latest revision as of 01:19, 25 March 2017
Description
Utility function to return a value from a named array or a specified default
<! removed transcluded page call, red link never existed >
Syntax
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
mixed The value from the source array
Defined in
libraries/joomla/utilities/arrayhelper.php
Importing
jimport( 'joomla.utilities.arrayhelper' );
Source Body
function getValue(&$array, $name, $default=null, $type='')
{
// Initialise 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 transcluded page call, red link never existed >
Examples
Code Examples