PHP json_encode() 함수 소개
PHP에 내장된 json_encode() 함수 사용하기(php > 5.2)을 사용하면 PHP에 있는 데이터를 다른 언어와 잘 옮겨서 사용할 수 있다.
이 함수의 기능은 숫자 값을 json 데이터 저장 형식으로 변환하는 것입니다.
01<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none><?php
04 'Name'=><codecourier new min-height:inherit color:blue background:none>'你妹'<codecourier new min-height:inherit background:none>,</codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table>
<p></p>
<table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>05<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none> <codecourier new min-height:inherit color:blue background:none>'Age'<codecourier new min-height:inherit background:none>=>22</codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table>
<p></p>
<table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>06<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none> <codecourier new min-height:inherit background:none>);</codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table>
<p></p>
<table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>07<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none> </tdcourier></codecourier></tdcourier></tr></tbody></table>
<p></p>
<table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>08<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit color:rgb background:none>$jsonencode <codecourier new min-height:inherit background:none>=
json_encode(<codecourier new min-height:inherit color:rgb background:none>$arr<codecourier new min-height:inherit background:none>);</codecourier></codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table>
<p></p>
<table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>09<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit color:rgb background:none>echo <codecourier new min-height:inherit color:rgb background:none>$jsonencode<codecourier new min-height:inherit background:none>;</codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table>
<p></p>
<table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>10<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none>?></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table>
<p>프로그램 실행 결과는 다음과 같습니다. </p>
<divcourier new min-height:inherit><p></p>
<p></p>
<table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>1<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none>{<codecourier new min-height:inherit color:blue background:none>"Name"<codecourier new min-height:inherit background:none>:null,<codecourier new min-height:inherit color:blue background:none>"Age"<codecourier new min-height:inherit background:none>:20}</codecourier></codecourier></codecourier></codecourier></codecourier></tdcourier></codecourier></tdcourier></tr></tbody></table> <p>json_encode 함수의 한자는 null로 인코딩되어 있습니다. 구글링해보니 프론트엔드와 긴밀하게 통합하기 위해 매우 간단하다는 것을 알았습니다. 프론트 엔드 Javascript도 utf-8이기 때문이라고 생각합니다. </p>
<divcourier new min-height:inherit><p></p>
<p></p>
<table><tbody><tr><tdcourier new border:0px outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:3em line-height:1.1em min-height:inherit color:rgb background:none><codecourier new min-height:inherit display:block background:none>1<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none><?php
4 'name'=>iconv('GB18030','utf-8','你妹'),
8echo json_encode($array);
이 프로그램을 실행한 결과는 다음과 같습니다.
1{"title":"u6d63u72b2ue785","body":"men"}
iconv는 형식을 변환할 때 GBK를 사용합니다. gb2312에서 보고된 오류는 GB2312에 6763개의 중국어 문자만 포함되어 있기 때문입니다. 수집되지 않은 한자가 나타나면 iconv에서 오류를 보고합니다. GBK는 GB2312-80과 완전히 호환되며 더 많은 문자를 지원하지만 여전히 오류가 발생합니다. GB18030은 기본적으로 GBK와 호환되며 100개 이상의 글꼴을 확장합니다. 배열의 모든 중국어 문자가 누락되었거나 json_encode 뒤에 u6d63 등이 나타납니다.
해결 방법은 urlencode() 함수를 사용하여 다음을 처리하는 것입니다. json_encode 이전에 urlencode()를 사용하여 모든 배열의 모든 내용을 처리한 다음 json_encode()를 사용하여 json 문자열로 변환합니다. 마지막으로 urldecode()를 사용하여 인코딩된 중국어를 다시 변환합니다.
01<tdcourier new border-width:0px border-left-style:solid border-left-color:rgb outline:0px float:none vertical-align:top position:static left:auto top:auto right:auto bottom:auto height:auto width:auto line-height:1.1em min-height:inherit background:none><codecourier new min-height:inherit background:none><?php
02/**************************************************************
04 *
使用特定function对数组中所有元素做处理
05 *
@param string &$array 要处理的字符串
06 *
@param string $function 要执行的函数
07 *
@return boolean $apply_to_keys_also 是否也应用到key上
10 *************************************************************/
11function arrayRecursive(&$array, $function, $apply_to_keys_also =
false)
13 static $recursive_counter =
0;
14 if (++$recursive_counter >
1000) {
15 die('possible
deep recursion attack');
17 foreach ($array as $key => $value)
{
18 if (is_array($value))
{
19 arrayRecursive($array[$key], $function, $apply_to_keys_also);