How to use json_decode() and json_encode() in php_PHP tutorial
1.json_decode()
json_decode
(PHP 5 >= 5.2.0, PECL json >= 1.2.0)
json_decode — for JSON Format string is encoded
Description
mixed json_decode (string $json [, bool $assoc])
Accepts a JSON format string and converts it into a PHP variable
Parameters
json
The string in json string format to be decoded.
assoc
When this parameter is TRUE, an array will be returned instead of an object.
Returns an object or if the optional assoc parameter is TRUE, an associative array is instead returned.
Example
Example #1 json_decode Example of ()
var_dump(json_decode($json));
var_dump(json_decode($ json, true));
?>
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int (3)
["d"] => int(4)
["e"] => int(5)
}
echo json_decode($data);
2.json_encode()
json_encode (PHP 5 >= 5.2.0, PECL json >= 1.2.0)
json_encode — JSON encoding of variables
Report a bug description
string json_encode ( mixed $value [, int $options = 0 ] )
Returns the JSON form of value
Report a bug Parameters
value
The value to be encoded, except the resource type, can be any data type
This function can only accept UTF-8 encoded data
options
A binary mask composed of the following constants Code: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT, JSON_UNESCAPED_UNICODE.
Report a bug Return value
If the encoding is successful, a String representation as JSON or returned on failure FALSE.
Report a bug Update log
Version Description
5.4.0 options parameters added constants: JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, and JSON_UNESCAPED_UNICODE.
5.3.3 The options parameter adds a constant: JSON_NUMERIC_CHECK.
5.3.0 Add options parameter.
Report a bug Example
Example #1 A json_encode() example
$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4, 'e'=>5);
echo json_encode($arr);
?>
The above routine will output:
{"a":1,"b":2,"c":3,"d":4, "e":5}
Example #2 Usage of options parameter in json_encode() function
$a = array('
echo "Normal: ", json_encode($a), "n";
echo "Tags: ", json_encode($a, JSON_HEX_TAG), "n";
echo "Apos: " , json_encode($a, JSON_HEX_APOS), "n";
echo "Quot: ", json_encode($a, JSON_HEX_QUOT), "n";
echo "Amp: ", json_encode($a, JSON_HEX_AMP) , “n”; , "nn";
$b = array();
echo "Empty array output as array: ", json_encode($b), "n";
echo "Empty array output as object: ", json_encode($b, JSON_FORCE_OBJECT), "nn";
$c = array(array(1,2,3));
echo "Non- associative array output as array: ", json_encode($c), "n";
echo "Non-associative array output as object: ", json_encode($c, JSON_FORCE_OBJECT), "nn";
$d = array('foo' => 'bar', 'baz' => 'long');
echo "Associative array always output as object: ", json_encode($d), "n";
echo "Associative array always output as object: ", json_encode($d, JSON_FORCE_OBJECT), "nn";
?>
The above routine will output :
Apos: ["
Quot: ["
Amp: ["
Unicode: ["
All: ["u003Cfoou003E","u0027baru0027","u0022bazu0022","u0026blongu0026","é"]
Empty array output as array: []
Empty array output as object: {}
Non-associative array output as array: [[1,2,3]]
Non- associative array output as object: {"0":{"0":1,"1":2,"2":3}}
Associative array always output as object: {"foo":" bar","baz":"long"}
Associative array always output as object: {"foo":"bar","baz":"long"}
Example #3 Examples of continuous and non-continuous arrays
$sequential = array("foo", "bar", "baz", "blong");
var_dump(
$sequential,
json_encode($sequential)
);
echo PHP_EOL."non-sequential array".PHP_EOL;
$nonsequential = array(1=>"foo", 2=>"bar", 3=>"baz" , 4=>"blong");
var_dump(
$nonsequential,
json_encode($nonsequential)
);
echo PHP_EOL."Delete a continuous array value Non-continuous array generated by ".PHP_EOL;
unset($sequential[1]);
var_dump(
$sequential,
json_encode($sequential)
);
? >
The above routine will output:
[0]=>
string(3) "foo"
[1]=>
string(3) "bar"
[2]=>
string(3) "baz"
[3]=>
string(5) "blong"
}
string(27) "["foo ","bar","baz","blong"]"
non-continuous array
array(4) {
[1]=>
string(3) "foo "
[2]=>
string(3) "bar"
[3]=>
string(3) "baz"
[4]=>
string(5) "blong"
}
string(43) "{"1":"foo","2":"bar","3":"baz","4":" blong"}"
A non-continuous array generated by deleting a continuous array value
array(3) {
[0]=>
string(3) "foo"
[2]=>
string(3) "baz"
[3]=>
string(5) "blong"
}
string(33) "{ "0":"foo","2":"baz","3":"blong"}"
$obj->Name= 'a1';$obj->Number ='123';
$obj->Contno= '000';
echo json_encode($obj ; :"a1",
"Number":"123",
}
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/325399.html
TechArticle
1.json_decode() json_decode (PHP 5 = 5.2.0, PECL json = 1.2.0) json_decode — Right JSON format string encoding description mixed json_decode ( string $json [, bool $assoc ] ) accepts...

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1
Powerful PHP integrated development environment