Saturday, September 4, 2010

Remove array elements useful PHP array functions

php array remove blank it from the elements (including elements of only blank characters) to a two-dimensional array into hashmap ** * remove the blank from the array elements (including elements of only whitespace characters) * * @ param array arr * @ param boolean trim * / CODE: functiona ...
php remove it from the array of empty elements (including elements of only whitespace characters) into a two-dimensional array of hashmap

**
* Remove the blank from the array elements (including elements only whitespace)
*
* @ Param array arr
* @ Param boolean trim

* /


CODE:
function array_remove_empty (& arr, trim = true)
(
foreach (arr as key => value) (
if (is_array (value)) (
array_remove_empty (arr [key]);
) Else (
value = trim (value);
if (value =='') (
unset (arr [key]);
) Elseif (trim) (
arr [key] = value;
)
)
)
)

/ **
* To convert a two-dimensional array of hashmap
*
* If omitted valueField parameters, then convert the result that each contain all the data for the array.
*
* @ Param array arr
* @ Param string keyField
* @ Param string valueField
*
* @ Return array
* /


CODE:
function array_to_hashmap (& arr, keyField, valueField = null)
(
ret = array ();
if (valueField) (
foreach (arr as row) (
ret [row [keyField]] = row [valueField];
)
) Else (
foreach (arr as row) (
ret [row [keyField]] = row;
)
)
return ret;
)

No comments:

Post a Comment