How to Encode Array in JSON PHP ?
Last Updated :
06 May, 2024
Encoding arrays into JSON format is a common task in PHP, especially when building APIs or handling AJAX requests.
Below are the approaches to encode arrays into JSON using PHP:
Using json_encode()
PHP provides a built-in function json_encode() to encode PHP data structures, including arrays, into JSON format. It automatically handles most data types, making it simple and efficient.
Example: The example below shows How to encode an array in JSON PHP Using json_encode().
PHP
<?php
// code
$array = ["name" => "John", "age" => 30, "city" => "New York"];
echo json_encode($array);
?>
Output{"name":"John","age":30,"city":"New York"}
Encoding Associative Arrays
Associative arrays, also known as key-value pairs, are commonly used in PHP. When encoding associative arrays into JSON, the keys become the property names, and the values become the property values in the resulting JSON object.
Example: The example below shows How to encode an array in JSON PHP Using Encoding Associative Arrays.
PHP
<?php
// code
$assocArray = ["name" => "Jane", "age" => 25, "city" => "Los Angeles"];
echo json_encode($assocArray);
?>
Output{"name":"Jane","age":25,"city":"Los Angeles"}
Custom JSON Serialization
In this approach, PHP jsonSerialize() method allows you to implement custom JSON serialization for your objects. In some cases, you may need more control over the JSON output, such as converting objects or complex data structures into JSON.
Example: The example below shows How to encode an array in JSON PHP Using Custom JSON Serialization.
PHP
<?php
class Person implements \JsonSerializable
{
public $name;
public $age;
public function jsonSerialize()
{
return [
"name" => $this->name,
"age" => $this->age,
];
}
}
$person = new Person();
$person->name = "Alice";
$person->age = 35;
echo json_encode($person);
?>
Output{"name":"Alice","age":35}
Similar Reads
How to JSON Decode in PHP? JSON Decode is the process of converting a JSON string into a PHP variable, typically an object or an associative array. Then we will print that decode JSON text. Below are the approaches to JSON decoding in PHP: Table of Content Using json_decode() methodUsing Explode and Foreach LoopUsing json_dec
2 min read
How to generate Json File in PHP ? In this article, we are going to generate a JSON file in PHP by using an array. JSON stands for JavaScript object notation, which is used for storing and exchanging data. JSON is text, written with JavaScript object notation. Structure: {"data":[ { "sub_data1":"value1", "sub_data2":"value2","sub_dat
3 min read
How to parse a JSON File in PHP? Parsing JSON in PHP is simple and efficient. JSON (JavaScript Object Notation) is a lightweight format used to store data in key-value pairs. PHP offers built-in functions to handle JSON encoding and decoding, making it easy to work with JSON data.JSON Syntax OverviewThe general syntax of a JSON obj
3 min read
How to Encode and Decode a URL in PHP? Encoding and decoding URLs in PHP are essential tasks when working with web applications, especially for handling user input and constructing query strings. URL encoding ensures that special characters are properly represented in URLs, preventing errors or unexpected behavior during data transmissio
2 min read
PHP | json_encode() Function The json_encode() function is an inbuilt function in PHP which is used to convert PHP array or object into JSON representation. Syntax : string json_encode( $value, $option, $depth ) Parameters: $value: It is a mandatory parameter which defines the value to be encoded. $option: It is optional parame
2 min read