How to convert php array to json object with example?

Today, We want to share with you php convert array to json.In this post we will show you php string to json, hear for how to get data from json array in php? we will give you demo and example for implement.In this post, we will learn about json to array php with an example.

Convert PHP Object To JSON | PHP Array To JSON

Example 1:



Example 2:



Example 3:

'Pankil', 'website'=>''];
  $jsonDetails = json_encode($details);
  echo $jsonDetails;



Convert JSON To PHP Array

$json = json_encode($player);

// print the array


    [name] => Pankil
    [email] => [email protected]

Convert PHP Object to JSON

name = $name;

  function set_color($color){
      $this->color = $color;

  function get_name() {
    return $this->name;

  function get_color(){
      return $this->color;

$iphone = new Product();

echo print_r($iphone);
Product Object
    [name] => Iphone
    [color] => White
echo json_encode($iphone);



Convert PHP Array to JSON

First create an array.

$player = array('name' => 'Pankil', 'email' => '[email protected]');
 $player = array('name' => 'Pankil', 'email' => '[email protected]');


 echo "---------------- \n";

 echo json_encode($player);


    [name] => Pankil
    [email] => [email protected]
{"name":"Pankil","email":"[email protected]"}

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

Leave a Comment