This article mainly introduces how to convert phpjson into an array for code sharing. if you need code, refer to a class and method for converting json into an array, in fact, the write 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.