All pastes #46912 Raw Edit

bewest

public php v1 · immutable
#46912 ·published 2006-03-25 06:41 UTC
rendered paste body
function php2js($data, $array_keys = array(), $level = 0, $isarray = false, $key_path = ""){  $reservedwords = array("if", "for", "class", "foreach", "in", "!xsi:nil");  $jsarray = $isarray ? '[ ' : '{ ';  $recs = array();  $array_str = "%s'%s'";  $value_str = "%s%s: '%s'";  $cur_key_path = $key_path;  if(is_array($data)) {    foreach($data as $key=>$value) {      $key_path = $cur_key_path . '/' . $key;      if(in_array($key, $reservedwords, true))        continue;      if(is_array($data[$key])) {        $recs[] =  ($isarray ? str_repeat("\t", $level) : $key . " : ") .          php2js($data[$key], $array_keys, $level + 1,            in_array($key_path, $array_keys), $key_path);      } else {        if($isarray) {          $recs[] = sprintf($array_str, str_repeat("\t", $level), c($value));        } else {          $recs[] = sprintf($value_str, str_repeat("\t", $level), $key, c($value));        }      }    }    $jsarray .=  "\n\t" . implode(",\n\t", $recs) . "\n";  }  $jsarray .= str_repeat("\t", $level) . ($isarray ? ']' : '}');  return $jsarray;}