note 21442 deleted from ref.array by maxim

From: Date: Tue, 12 Nov 2002 16:06:14 +0000
Subject: note 21442 deleted from ref.array by maxim
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-39382@lists.php.net to get a copy of this message
I bet there was someone else that needed to translate an associative array from php into, lets say, an associative array in JavaScript (or other programing language) You can use these functions i wrote, which i tried to be as customizable as possible, for use in many other languages. For example, in JavaScript an associative array would look like: array: { category1: { title: 'cat title 1', name: 'cat_name_1', desc: 'cat desc 1' }, category2: { title: 'cat title 2', name: 'cat_name_2', desc: 'cat desc 2' }, property: value } Here are the functions i wrote: (legend at the end) function ga($arr, $sep, $grp, $elemsep, $end, $ind, $indpos) { $newarr = ""; foreach( $arr as $key=>$val ) { if( is_array( $val ) ) { $newarr .= str_repeat( $ind, $indpos ).$key.$sep.$grp[0].$end; $newarr .= ga( $val, $sep, $grp, $elemsep, $end, $ind, $indpos + 1 ); $newarr .= str_repeat( $ind, $indpos ).$grp[1]; } else $newarr .= str_repeat( $ind, $indpos ).$key.$sep.$val; $newarr .= ( ( $arr[$key] == end( $arr ) ) ? "" : $elemsep ) . $end; } return $newarr; } function genarr( $arrname, $genfunc, $sep = ": ", $grp = array( "{", "}" ) , $elemsep = ",", $end = "\n", $ind = "\t", $indpos = 1 ) { global $$arrname; return $arrname.$sep.$grp[0].$end.$genfunc ($$arrname, $sep, $grp, $elemsep, $end, $ind, $indpos).$grp[1]; } Legend: $arr = the php associative array to transform $sep = the separator char/string between the key and the value (in JavaScript its ":", in PHP its "=>") $grp = an array having the 1st element as the "new array" character and the 2nd element as the "array end" character (in javaScript they are "{" and "}", in PHP they are "array(" and ")" ) $elemsep = elements separator (in javascript and PHP its "," ) $end = the caracter appended at the end of an element $ind = indentation character $indpos = default indent NOTE: if you would like an inline output, then set the $end = "" and the $ind = "". If you want a stylish output you can set the $end = "\n" and $ind = "\t". $genfunc = a STRING evaluating to the the fetching function name (in this case "ga") $arrname = a STRING, evaluating to the array variable name (note the calling to it by "$$arrname" ) NOTE: for JavaScript, i set all arguments by default in the "genarr" function. EXAMPLE: Supposing you have a PHP associative array: $bob = array( "key1" => array ( ...... then for javaScript, you should call the "genarr" function like this: $JSArray = genarr( "bob", "ga" ) note the arguments: "bob" and "ga" This will result in a Javascript array like this: bob: { key1: { ..... Hope it helped because i need to have a huge JavaScript associative array for one of the sites i run but also the same array in PHP and its a hell of a job updating 2 pieces of code when PHP can generate it dynamically, thus its sufficient to make changes only in the PHP code. Cheers P.S. let me know if you found it useful

« previous php.notes (#39382) next »