本文實例講述了php實現(xiàn)數(shù)組中索引關(guān)聯(lián)數(shù)據(jù)轉(zhuǎn)換成json對象的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
public static function encode(& $var ) { return '{' .implode( ',' ,self::encodeExcute( $var )). '}' ; } private static function encodeExcute(& $var ) { $json = array (); switch ( gettype ( $var )) { case 'array' : foreach ( $var as $key => $value ) { if ( is_array ( $value )) { $json [] = '"' . $key . '":{' .implode( ',' ,self::encodeExcute( $value )). '}' ; //$json[$key] = self::encode($value); } elseif ( is_object ( $value )) { $json [] = "\\" { $key }\\ ":" .json_encode( $value ->attributes); } else { echo 'ERROR 未知類型,還未解析' ; Yii::app()-> end (); } } break ; case 'object' : break ; } return $json ; } |
希望本文所述對大家的php程序設(shè)計有所幫助。