note 21452 added to ref.array
| From: | bog2k at softhome dot net | Date: | Mon, 13 May 2002 20:51:24 +0000 |
| Subject: | note 21452 added to ref.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-30599@lists.php.net to get a copy of this message | ||
In the last post i missed something in the fetching function. It had troubles handling boolean
values and i forgot strings as well. Also, detecting the end of a sub-array didnt work as expected.
Here are the new versions that worked with everything i threw at it:
function ga($arr, $sep, $grp, $elemsep, $str, $end, $ind, $indpos) {
$newarr = "";
$len = count( $arr );
for( $i=0; $i<$len; $i++ ) {
//get current key,value pair
list( $key, $val ) = each( $arr );
if( is_array( $val ) ) {
//calling recursively if it hits a sub-array
$newarr .= str_repeat( $ind, $indpos ).$key.$sep.$grp[0].$end;
$newarr .= ga( $val, $sep, $grp, $elemsep, $str, $end, $ind, $indpos + 1 );
$newarr .= str_repeat( $ind, $indpos ).$grp[1];
}
else {
$newarr .= str_repeat( $ind, $indpos ).$key.$sep;
//determine element type
$newarr .= ( is_bool( $val ) ) ? (($val) ? 'true' : 'false') : ( is_string(
$val ) ? $str.addslashes( $val ).$str : $val );
}
//if last element then don't add $elemsep after it
$newarr .= ( $i == $len-1 ) ? $end : $elemsep.$end;
}
return $newarr;
}
function genarr( $arrname, $genfunc, $sep = ': ', $grp = array( '{',
'}' ) , $elemsep = ',', $str = '"', $end = "\r\n", $ind
= "\t", $indpos = 1 ) {
global $$arrname;
return $arrname.$sep.$grp[0].$end.$genfunc ($$arrname, $sep, $grp, $elemsep, $str, $end, $ind,
$indpos).$grp[1];
}
I also added addslashes() to string values. There is a new argument:
$str = the string delimiter (usually " \" " or " \' " ). The second
function only has the new argument modified. The rest is the same.
Please let me know if you find anything going wrong.
Cheers
--
http://www.php.net/manual/en/ref.array.php
http://master.php.net/manage/user-notes.php?action=edit+21452
http://master.php.net/manage/user-notes.php?action=delete+21452
http://master.php.net/manage/user-notes.php?action=reject+21452