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

How can php handle json strings

藏色散人
Release: 2023-02-28 11:34:02
Original
2896 people have browsed it

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));
}
Copy after login

The output is

object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
Copy after login

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;";
?>
Copy after login

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;";
//也可得到相同的结果
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template