PHP json is converted into an array for code sharing, which is a json Array. PHP json is converted into an array for code sharing. json written in the json Array is converted into an array class and method, in fact, the write method can convert most data structures containing json strings into arrays in php json format for code sharing, json Array
The written json is converted into an array class and method. In fact, the writing method can convert most data structures containing json strings into arrays. the code is as follows:
The code is as follows:
Class antiTranJson
{
Protected static function jsonToArray ($ json)
{
If (! Is_string ($ json) | is_null (json_decode ($ json, true )))
Throw new NotJsonStringException ('param is not a json string ');
$ DeJson = json_decode ($ json, true );
Return self: toArray ($ deJson );
}
Protected static function stdClassToArray ($ stds)
{
If (is_object ($ stds ))
Throw new NotObjectException ('params not object ');
$ Params = get_object_vars ($ stds );
Return self: toArray ($ params );
}
Protected static function arrayRToArray ($ params)
{
$ Tmp = array ();
If (! Is_array ($ params ))
Throw new NotArrayException ('params not array ');
Foreach ($ params as $ k => $ v)
{
$ Tmp [$ k] = self: toArray ($ v );
}
// Var_dump ($ tmp );
Return $ tmp;
}
// Call this method to convert all json data.
Public static function toArray ($ params)
{
$ Tmp = array ();
If (is_string ($ params )&&! Is_null (json_decode ($ params )))
$ Tmp = self: jsonToArray ($ params );
Elseif (is_array ($ params ))
$ Tmp = self: arrayRToArray ($ params );
// Note that $ params is an object and can be converted only when its attributes are readable (public or temporary object attributes ).
Elseif (is_object ($ params ))
$ Tmp = self: stdClassToArray ($ params );
Else
$ Tmp = $ params;
Return $ tmp;
}
The above is the relevant code. at least we can use it for the time being. if you have good suggestions, I hope you can discuss and make progress together. thank you.
Http://www.bkjia.com/PHPjc/909345.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/909345.htmlTechArticlephp json converted into array form code sharing, json Array written json converted into an array of classes and methods, in fact write method can include most of the json string data structure conversion...