HDTuto.com

PHP Convert Array to Json Object Example



This article will provide some of the most important example php convert object to array json_encode. we will help you to give example of php json_decode convert object to array. This tutorial will give you simple example of convert php array or object to json string. you will learn php convert array to json object. Here, Creating a basic example of php convert json object to associative array.

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'=>'itsolutionstuff.com'];

$jsonDetails = json_encode($details);

echo $jsonDetails;

?>

Output:

{"name":"Hardik","website":"itsolutionstuff.com"}

I hope it can help you...




May 2, 2020 | Category : PHP



Blog Search