[eluser]ntheorist[/eluser]
just started picking up CI, and thought i'd throw out some functions i use on arrays that have proved really helpful (mainly for debugging).
I amended these to the Array helper file for use in CI (system/helpers/array_helper.php)
Code:
// Prints an array in a readable format
if ( ! function_exists('print_array'))
{
function print_array($a,$btag="",$etag="") {
if(is_array($a)) {
printf("<table cellpadding=0 cellspacing=0>");
while(list($one,$two)=each($a)) {
printf("\n<tr valign=baseline><td>$btag$one$etag</td><td>".
" $btag=>$etag</td>".
"<td align=right> %s</td></tr>\n"
,sprint_array($two,$btag,$etag));
}
printf("</table>");
}
else {
printf("%s%s%s",$btag,$a,$etag);
}
}
}
// Same as print_array but returns the string instead of printing it
if ( ! function_exists('sprint_array'))
{
function sprint_array($a,$btag="",$etag="") {
if(is_array($a)) {
$out=sprintf("<table cellpadding=0 cellspacing=0>");
while(list($one,$two)=each($a)) {
$out .= sprintf("\n<tr valign=baseline><td>$btag$one$etag</td><td>".
" $btag=>$etag</td>".
"<td align=right> %s</td></tr>\n"
,sprint_array($two,$btag,$etag));
}
$out .= "</table>";
return $out;
}
else {
return sprintf("%s%s%s",$btag,$a,$etag);
}
}
}
// Returns TRUE if array is a valid array and contains at least one element
if ( ! function_exists('valid_array'))
{
function valid_array($arr) {
if(is_array($arr) && count($arr) > 0) {
return 1;
} else {
return 0;
}
}
}
example:
Code:
$a = array('one','two','three'=>3, 'four' => array('five','six','seven'));
print_array($a);
outputs:
Code:
0 => one
1 => two
three => 3
four =>
0 => five
1 => six
2 => seven
cheers!
cc