PHP json_decode() Function

P
PHP wordwrap() Function
PHP json_encode() Function

In this article, you will learn how to encode a value to JSON format. The PHP method JSON_ENCODE() convert the given value to JSON format. The given value turns into a JSON object. JSON_ENCODE() function can encode both indexed as well as associative arrays.

What is the syntax of the function in php?

examples of the function

Example 1. In this example, we store JSON data in a PHP variable, and then decode it into a PHP object.

<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';
var_dump(json_decode($jsonobj));
?>

Example 2. In this example, we store JSON data in a PHP variable, and then decode it into a PHP associative array.

<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';
var_dump(json_decode($jsonobj, true));
?>

Example 3. In this example, we access the values from the PHP object.

<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';

$obj = json_decode($jsonobj);

echo $obj->Peter;
echo $obj->Ben;
echo $obj->Joe;
?>

Example 4. In this example, we access the values from the PHP associative array.

<?php
$jsonobj = '{"Peter":35,"Ben":37,"Joe":43}';

$arr = json_decode($jsonobj, true);

echo $arr["Peter"];
echo $arr["Ben"];
echo $arr["Joe"];
?>
PHP wordwrap() Function
PHP json_encode() Function

Add comment

Tutor Network

Share this

Learn PHP from A to Z