0
0
PHPprogramming~30 mins

JSON encoding and decoding in PHP - Mini Project: Build & Apply

Choose your learning style9 modes available
JSON encoding and decoding
📖 Scenario: You are working on a simple PHP script that needs to handle user data. You will practice converting PHP arrays to JSON format and back, which is common when sending or receiving data in web applications.
🎯 Goal: Build a PHP script that creates an array of user information, converts it to a JSON string, then decodes the JSON back to a PHP array, and finally prints the decoded data.
📋 What You'll Learn
Create a PHP array with specific user data
Create a variable to hold the JSON encoded string
Decode the JSON string back to a PHP array
Print the decoded array in a readable format
💡 Why This Matters
🌍 Real World
Web applications often send and receive data in JSON format between the server and client. Knowing how to encode and decode JSON in PHP is essential for handling API responses and requests.
💼 Career
Many PHP developer roles require working with JSON data for REST APIs, AJAX calls, and configuration files. This skill is fundamental for backend and full-stack developers.
Progress0 / 4 steps
1
Create the user data array
Create a PHP array called user with these exact key-value pairs: 'name' => 'Alice', 'age' => 30, and 'city' => 'New York'.
PHP
Need a hint?

Use square brackets to create the array and assign the keys and values exactly as shown.

2
Encode the array to JSON
Create a variable called jsonString and set it to the JSON encoded string of the user array using json_encode().
PHP
Need a hint?

Use the json_encode() function and assign the result to $jsonString.

3
Decode the JSON string back to an array
Create a variable called decodedUser and set it to the result of decoding jsonString back to a PHP array using json_decode() with the second parameter set to true.
PHP
Need a hint?

Use json_decode() with the second argument true to get an associative array.

4
Print the decoded array
Use print_r() to print the decodedUser array so it is readable.
PHP
Need a hint?

Use print_r($decodedUser); to display the array in a readable format.