How to convert php array to json object with example?

How to convert php array to json object with example?

In this post we will give you information about How to convert php array to json object with example?. Hear we will give you detail about How to convert php array to json object with example?And how to use it also give you demo for it if it is necessary.

In this example, we will lean to how to convert json objects of array into the php array. we will convert php array into json string. we can convert json object to associative array in php using json_encode. we can also force convert json object by “JSON_FORCE_OBJECT” parameter.

We many times require to convert php array to json array in php application like if you are creating any web services. you always need to send data as json response. Also when you are working with ajax request at that time also you need to send json response because it will easily to getting data and display them.

In this tutorial, you can see bellow example with simple way to converting json object of php array example. also with force convert json object and php convert json object to associative array. So let’s there are three example so you can use anything that you require.

Example 1:

<?php

$languages = ['PHP', 'Java', 'JQuery', '.Net', 'Javascript'];

$languagesJSON = json_encode($languages);

echo $languagesJSON;

?>

Output:

["PHP","Java","JQuery",".Net","Javascript"]

Example 2:

<?php

$languages = ['PHP', 'Java', 'JQuery', '.Net', 'Javascript'];

$languagesJSONObject = json_encode($languages, JSON_FORCE_OBJECT);

echo $languagesJSONObject;

?>

Output:

{"0":"PHP","1":"Java","2":"JQuery","3":".Net","4":"Javascript"}

Example 3:

<?php

$details = ['name'=>'Hardik', 'website'=>'onlinecode'];

$jsonDetails = json_encode($details);

echo $jsonDetails;

?>

Output:

Also see:How to convert array values to lowercase in PHP?

{"name":"Hardik","website":"onlinecode"}

I hope it can help you…

Hope this code and post will helped you for implement How to convert php array to json object with example?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

3  +  7  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US