How To Convert Php Array To Json Object With Example?

JSON stands for JavaScript Object Notation. It is data saved in the .json file & consists of a series of key/value pairs. JSON is used lớn transfer the data between the VPS & the browser. Here is a primary example of what might be in a .json string.

Bạn đang xem: How to convert php array to json object with example?

Arrays in PHPhường. will also be converted inlớn JSON when using the PHP.. function json_encode(). You can find the example of covert PHP Array to JSON.

If you’re passing JSON data lớn a javascript program, make sure your program begins with:

PHP json_encode()

PHP.. json_encode() is an inbuilt function that converts a PHPhường. value to lớn JSON value. Objects in PHPhường. can be converted into lớn JSON by using the PHPhường function called json_encode(). The json_encode() function returns the string, if the function works.


The syntax of json_encode() function is following.

json_encode(value, options)The value parameter is required & of type mixed.


The json_encode() function returns a string, if the function works.

Let’s see the following example.

1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);emang đến json_encode($arr)." ";
The output is the following.


Convert PHP. Object khổng lồ JSON 

PHP json_encode() method can convert PHPhường Object to JSON. So objects in PHPhường can easily convert into JSON using PHP. json_encode() method.

title = 'Harry Potter Game';$app->price = 20;$jsonData = json_encode($app);emang lại $jsonData." ";So, we have defined one class App & then created an object and set the properties and convert the object to JSON and print that JSON output. The output is the following.


Convert PHP String to JSON

PHP json_encode() function is useful to convert String to lớn JSON object .

// tiện ích.php$str = "hello";emang đến json_encode($str)." ";The output will be the same as the string.

You can use the json_encode() in any PHPhường Framework.

Xem thêm: Sửa Lỗi Tv Tcl - Các Lỗi Thường Gặp Của Tivi Tcl Và Cách Sửa Chữa

If the array keys in your PHPhường array are not consecutive sầu numbers, json_encode() must make the other construct an object since JavaScript arrays are always consecutively numerically indexed.

Use array_values() on the outer structure in PHP to discard the original array keys and replace them with zero-based consecutive sầu numbering.

PHP.. is a server-side programming language and should be used for operations that can only be performed by a VPS, lượt thích accessing a database.

How khổng lồ convert PHP array lớn JSON

We can convert PHP.. array to lớn json using the json_encode() function.

See the following code.

'Eleven', 'Mike' => 'Finn', 'Gaten' => 'Dustin', 'Noah' => 'Will'); emang đến json_encode($arr)." ";See the output.

➜ pro php tiện ích.php"Millie":"Eleven","Mike":"Finn","Gaten":"Dustin","Noah":"Will"➜ pro

Convert Multidimensional PHP. Array into lớn JSON

Let’s see an example where we can encode the multidimensional array.

array( 'item_type_id' => 1, 'string_key' => 'AA', 'string_value' => 'Hello', 'string_extra' => 'App', 'is_public' => 1, 'is_public_for_contacts' => 0 ));eđến json_encode($post_data)." ";The output of the above sầu code is following.


Generally, you have sầu to use json_encode() function when you need lớn sover an AJAX request lớn the VPS because JSON data is useful to lớn transfer between client và VPS. So converting PHP.. khổng lồ JSON, Javascript to lớn JSON is easy. That is why the JSON object contributes lớn a significant role in today’s web development.

The reverse process of json_encode is json_decode().

Decoding JSON data is as simple as encoding it. You can use a PHP json_decode() function to lớn convert a JSON encoded string into the appropriate PHP data type.


JSON encode decode is one of the most frequently required operations. In this article, we have sầu seen how to encode and decode JSON using PHP.. and seen two inbuilt functions to lớn perform the various operations. Those are,


We have sầu seen how to lớn convert PHP array lớn json & PHPhường. object lớn json using PHP json_encode() function.