PHP dimensional array to Javascript array WITHOUT JSON -
i'm trying convert php multidimensional array javascript array without using json encoder because of version of server.
exemple of multidimensional array :
array ( [0] => array ( [0] => 18 [1] => région grand est [2] => ge ) [1] => array ( [0] => 17 [1] => région grand ouest / nord [2] => go n ) [2] => array ( [0] => 25 [1] => région grand ouest / sud [2] => go s ) )
currently no multidimensional array i'm using function :
function js_str($s) { return '"' . addcslashes($s, "\0..\37\"\\") . '"'; } function js_array($array) { if (is_array($array)) { $temp = array_map('js_str', $array); return '[' . implode(',', $temp) . ']'; } return '[-1]'; }
but can't use multidimensional, i'm trying somthing similar recursively size of array.
to result :
myarray = [[18, 'région grand est', 'ge'],[17, 'grand ouest / nord', 'go n'], [25, 'région grand ouest / sud', 'go s']];
it's hard find answer without json_encode, help. (yes i'm developping on prehistoric server)
i approach problem recursive function this:
function js_array($array) { if (is_array($array)) { $temp = array(); $output = '['; foreach ($array $key=>$value) { $temp[] .= "'$key':" . js_array($value); } $output .= implode(',', $temp); $output .= "]"; } else { $output .= "'$array'"; } return $output; }
what we're doing here evaluating each element of array see if array. each level drills down until left simple key:value pairs.
you can edit special characters or drop array keys if want.
Comments
Post a Comment