PHP json transform array form code sharing _php Tips

Source: Internet
Author: User
Tags data structures php json

The written JSON converts a class and method of an array, and in fact writes a method that converts most of the data structures that contain JSON strings to the array, on the code:

Copy Code code as follows:

Class Antitranjson
{
protected static function Jsontoarray ($json)
{
if (!is_string ($json) | | is_null (Json_decode ($json, True))
throw new Notjsonstringexception (' param ' 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, the data containing JSON can be converted
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);
Notice here that if $params is an object, the conversion can only be achieved if the contained 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 for the time being used still can be, if you have good suggestions, I hope you discuss the discussion, common progress, thank you

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.