PHP convert indexed array to JSON

Source: Internet
Author: User
Tags php json yii
This paper mainly introduces the method of transforming Index association data into JSON object in PHP, and analyzes the conversion technique of PHP array and JSON format data based on YII framework. We hope to help you.

The implementation method is as follows:


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 unknown type, not yet resolved ';     Yii::app ()->end ();    }   }   break;  Case ' object ': Break   ;} return $json;}

Related recommendations:

PHP solve JSON Chinese display problem

A sample usage of PHP JSON-related functions

PHP JSON vs. XML serialization/deserialization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.