convert array to json php – How to convert php array to json object with example?

convert array to json php – Convert the request into an object, using the PHP function json_decode(). json_encode is available in php > 5.2.0: echo json_encode ($dataResult);

convert array to json php

The json_encode() function is used to encode a value to JSON format. It is an inbuild php function that is used to convert PHP array or object into JSON representation.

Example 1: Converting PHP result array to JSON

<?php
  
  $players = ['Jadeja', 'Iyer', 'Rahul', '.Net', 'Virat'];
  
  $playersJSON = json_encode($players);
  
  echo $playersJSON;
  
?>

Result

["Jadeja","Iyer","Rahul",".Net","Virat"]

Example 2: PHP json_encode: How to Convert PHP Array to JSON

<?php
  
  $players = ['Jadeja', 'Iyer', 'Rahul', '.Net', 'Virat'];
   
  $playersJSONObject = json_encode($players, JSON_FORCE_OBJECT);
   
  echo $playersJSONObject;
    
?>

Result

{"0":"Jadeja","1":"Iyer","2":"Rahul","3":".Net","4":"Virat"}

Example 3: Converting an array to JSON data with PHP

<?php
  
  $info = ['name'=>'Dipti', 'website'=>'www.pakainfo.com'];
  
  $jsoninfo = json_encode($info);
  echo $jsoninfo;
     
?>

Result

{"name":"Dipti","website":"www.pakainfo.com"}

Don’t Miss : PHP Converting An Array To JSON Tutorial With Example

I hope you get an idea about convert array to json php.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.