将数组加密转换json格式代码
时间:2010-11-23 来源:yangqing_fly
<?php
class json_encode {
public static function encode($arr) {
if (is_numeric ( key ( $arr ) )) {
$output = '[';
$last = count ( $arr );
for($i = 0; $i < $last; $i ++) {
if (is_array ( $arr [$i] )) {
$output .= self::encode ( $arr [$i] );
} else {
$output .= self::val ( $arr [$i] );
}
if ($i !== $last - 1) {
$output .= ',';
}
}
$output .= ']';
return $output;
} else {
$output = '{';
$last = count ( $arr );
$i = 0;
foreach ( $arr as $key => $val ) {
$i ++;
$output .= $key . ":";
if (is_array ( $val )) {
$output .= self::encode ( $val );
} else {
$output .= self::val ( $val );
}
if ($i !== $last) {
$output .= ',';
}
}
$output .= '}';
return $output;
}
}
private static function val($val) {
if (is_string ( $val )) {
return '"' . rawurlencode ( $val ) . '"';
} elseif (is_int ( $val )) {
return sprintf ( '%d', $val );
} elseif (is_float ( $val )) {
return sprintf ( '%F', $val );
} elseif (is_bool ( $val )) {
return ($val ? 'tree' : 'false');
} else {
return 'null';
}
}
}
echo json_encode::encode ( array ('a', 'b', 'c' ) );
echo "<br>";
echo json_encode::encode ( array ("ccc" => 'ak', "vvv" => 'bk', "bbb" => 'ck' ) );
$test = array(
array(
'name' => array('John', 'Smith'),
'age' => 27,
'sex' => 0,
'height' => 180.53,
'is_human' => true,
'string' => 'Hello',
),
array(
'name' => array('Green', 'Alien'),
'age' => 642,
'sex' => null,
'height' => 92.21,
'is_human' => false,
'string' => '文件的代码!', // test utf8 here
)
);
echo "<br>";
echo json_encode::encode ( $test);
?>
class json_encode {
public static function encode($arr) {
if (is_numeric ( key ( $arr ) )) {
$output = '[';
$last = count ( $arr );
for($i = 0; $i < $last; $i ++) {
if (is_array ( $arr [$i] )) {
$output .= self::encode ( $arr [$i] );
} else {
$output .= self::val ( $arr [$i] );
}
if ($i !== $last - 1) {
$output .= ',';
}
}
$output .= ']';
return $output;
} else {
$output = '{';
$last = count ( $arr );
$i = 0;
foreach ( $arr as $key => $val ) {
$i ++;
$output .= $key . ":";
if (is_array ( $val )) {
$output .= self::encode ( $val );
} else {
$output .= self::val ( $val );
}
if ($i !== $last) {
$output .= ',';
}
}
$output .= '}';
return $output;
}
}
private static function val($val) {
if (is_string ( $val )) {
return '"' . rawurlencode ( $val ) . '"';
} elseif (is_int ( $val )) {
return sprintf ( '%d', $val );
} elseif (is_float ( $val )) {
return sprintf ( '%F', $val );
} elseif (is_bool ( $val )) {
return ($val ? 'tree' : 'false');
} else {
return 'null';
}
}
}
echo json_encode::encode ( array ('a', 'b', 'c' ) );
echo "<br>";
echo json_encode::encode ( array ("ccc" => 'ak', "vvv" => 'bk', "bbb" => 'ck' ) );
$test = array(
array(
'name' => array('John', 'Smith'),
'age' => 27,
'sex' => 0,
'height' => 180.53,
'is_human' => true,
'string' => 'Hello',
),
array(
'name' => array('Green', 'Alien'),
'age' => 642,
'sex' => null,
'height' => 92.21,
'is_human' => false,
'string' => '文件的代码!', // test utf8 here
)
);
echo "<br>";
echo json_encode::encode ( $test);
?>
相关阅读 更多 +