Home >Backend Development >PHP Problem >How can php handle json strings

How can php handle json strings

藏色散人
藏色散人Original
2019-11-06 09:13:512910browse

How can php handle json strings

How can php handle json strings?

Code example:

<?php
$json = &#39;{"a":1,"b":2,"c":3,"d":4,"e":5}&#39;;
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 = &#39;{"a":1,"b":2,"c":3,"d":4,"e":5}&#39;;
$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 = &#39;{"a":1,"b":2,"c":3,"d":4,"e":5}&#39;;
$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn