I am working on some code to make working with arrays really flexible. It could be useful for a lot of stuff. Do you see ay problems with it? Or have any ideas for it? I figure it could work well as plugins for snippets like eForm events.
// example function call - parse the $_POST array
echo parse_arrays($_POST,'parseArray','','parseValues');
/*
Function: parse_arrays
vars are:
$arr - array to parse
$fn_array - function to run when the value is an array. Always passed the var $val
$fn_array_attr - additional variables to be passed to $fn_array
$fn_values = function to run when the value is not an array. Always passed the vars $key and $val
$fn_values_attr - additional variables to be passed to $fn_values
$wrapper - not used here. Could be used to wrap the results
$cycle - not used here. Could be used to see which 'dimension' of the array you are in.
*/
function parse_arrays($arr, $fn_array, $fn_array_attr, $fn_values, $fn_value_attr, $wrapper = 'data', $cycle = 1)
{
foreach($arr as $key => $val)
{
//if it's not an array
if(!is_array($val))
{
if (function_exists($fn_values))
{
$output .= $fn_values($key, $val, $fn_array_attr);
} else
{
$output .= $fn_values . 'does not exist<br/>';
}
}
//if it is an array
else
{
if (function_exists($fn_array))
{
$output .= $fn_array($val, $fn_array_attr);
}
else
{
$output .= $fn_array . 'does not exist<br/>';
}
//if it is an array
//$output.= parse_arrays($val);
}
}
return $output;
}
//array is passed to this function from parse_arrays $fn_array variable
function parseArray($arr)
{
$output = '<pre>';
$output .= print_r($arr, true);
$output .= '</pre>';
return $output;
}
//key and value is passed to this function from parse_arrays $fn_values variable
function parseValues($key, $val)
{
$output = $key .': '. $val .'<br/>';
return $output;
}
?>
It’s very interesting. You can see from my post that I’m not a developer, but I can see the value in what you’re doing and may be of use to me and my need to retrieve stock quotes. I may get back to you on it. Good work.