Home >Backend Development >PHP Problem >How can php handle json strings
How can php handle json strings?
Code example:
<?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; var_dump(json_decode($json)); }
The output is
object(stdClass)#1 (5) { ["a"] => int(1) ["b"] => int(2) ["c"] => int(3) ["d"] => int(4) ["e"] => int(5) }
php processing json string
<?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; $jsonArr = json_decode($json, TRUE); extract($jsonArr); //数组中将变量导入到以当前符号表 echo "a=$a;b=$b;c=$c;d=$d;e=$e;"; ?>
Also The resulting object can also be traversed just like an array.
<?php $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; $jsonObj = json_decode($json); foreach($jsonObj as $jk=>$jv) { $$jk = $jv; } echo "a=$a;b=$b;c=$c;d=$d;e=$e;"; //也可得到相同的结果 ?>
In addition, the value of the json object obtained through json_decode can also be obtained through "$jsonObj->a".
For more PHP related knowledge, please visit PHP Chinese website!
The above is the detailed content of How can php handle json strings. For more information, please follow other related articles on the PHP Chinese website!