How To Use Json Data In PHP Example

03-Apr-2023

.

Admin

How To Use Json Data In PHP Example

hi guys,

today i will explained how to use json data in php. this example is so easy to use in php.

json stands for javascript object notation,and is a syntax for storing and exchanging data.

since the json format is a text-based format, it can easily be sent to and from a server, and used as a data format by any programming language

php has some built-in functions to handle json to given two function in php.

-json_encode()

-json_decode()

so let's start to the example.

json_encode()


the json_encode() function is used to encode a value to json format.

<!doctype html>

<html>

<body>

<?php

$age = array("peter"=>35, "ben"=>37, "joe"=>43);

echo json_encode($age);

?>

</body>

</html>

output

{"peter":35,"ben":37,"joe":43}

json_decode()

the json_decode() function is used to decode a json object into a php object or an associative array

<!doctype html>

<html>

<body>

<?php

$jsonobj = '{"peter":35,"ben":37,"joe":43}';

print_r(json_decode($jsonobj));

?>

</body>

</html>

output

stdclass object ( [peter] => 35 [ben] => 37 [joe] => 43 )

#PHP 8

#PHP