crud ajax php mysql

PHP JSON_ENCODE EXAMPLE AND PHP JSON_DECODE EXAMPLE

In this article you would be learning how to convert php variables into JSON by using json_encode() and how to convert JSON into php variables by using json_decode().

What is JSON ?

JavaScript Object Notation.It is used to transmit data over a network connection.It is the subset of javascript syntex and language-independent.

It is a popular alternative to XML.

It is easy to read and understand for human and machine.

What is php json_encode ?

In php, php objects and php arrays are converted into JSON by using json_encode()

What is php json_decode ?

In php, JSON string is converted into php variable by using json_decode()

What is PHP OBJECT ?

An object is an instances of defined class.

We can create as many as objects for single class.Before creating object it is necessary to create class.

Class is the structure or blue-print that contains properties and methods.

To define or create class we use keyword called "class".


              <?php

                class boy
                 {
                   var $name="rahul";
                   var $age="30";

                }
                $boy = new boy();
                 echo $boy->name;
                 echo '<pre>';
                 echo $boy->age
                ?>

              

OUTPUT

rahul

30

What is PHP ARRAY ?

It is used to hold more than one values at a time.

In php, array() function is used to create an array.

In php, there are three types of array:-

  • Indexed array
  • Associative array
  • Multidimensional array

Indexed Array?

By default, All the elements in an array is represented by index values starting from 0.


            <?php
            $product = array("fruits", "vegitables", "fast food");
            echo $product[0];
            echo'
'; echo $product[1]; echo'
'; echo $product[2]; ?>

OUTPUT FOR INDEXED ARRAY

fruits

vegitables

fast food

Associative Array?

It contains key/value pairs. Each key and value is seperated by "=>" symbol.

In this type of array, "key" is used to access the value.


      <?php
      $person = array("name"=>"rahul", "age"=>"37", "salary"=>"43000");
      echo "name : " . $person["name"];
      echo'
'; echo "age : " . $person["age"]; echo'
'; echo "salary : " . $person["salary"]; ?>

OUTPUT FOR ASSOCIATED ARRAY

name : rahul

age : 37

salary : 43000

Multi-Dimensional Array?

It is an array which contains nested array(array inside another array).

It is very helpful in storing values with more than one key.


    <?php
    $products = array
      (
      array("fruits",10),
      array("vegitables",15),
      array("fast food",5)
      );

      echo $products[0][0]. "  and  " .$products[0][1];
      echo '
'; echo $products[1][0]. " and " .$products[1][1]; echo '
'; echo $products[2][0]. " and " .$products[2][1]; ?>

OUTPUT FOR MULTIDIMENSIONAL ARRAY

fruits and 10

vegitables and 15

fast food and 5

LET'S CONVERT PHP OBJECT TO JSON


  <?php
  class boy
   {
     var $name="rahul";
     var $age="30";

  }
  $boy = new boy();
    $js=json_encode($boy);
    echo $js."\n";
 ?>
    

OUTPUT FOR OBJECT TO JSON

{"name":"rahul","age":"30"}

LET'S CONVERT PHP INDEXED ARRAY TO JSON


<?php
$product = array("fruits", "vegitables", "fast food");
$js=json_encode($product);
echo $js."\n";
?>
  

OUTPUT FOR INDEXED ARRAY TO JSON

["fruits","vegitables","fast food"]

LET'S CONVERT PHP ASSOCIATIVE ARRAY TO JSON


   <?php
   $person = array("name"=>"rahul", "age"=>"37", "salary"=>"43000");
   $js=json_encode($person);
   echo $js."\n";
   ?>
   

OUTPUT FOR ASSOCIATIVE ARRAY TO JSON

{"name":"rahul","age":"37","salary":"43000"}

LET'S CONVERT PHP MULTIDIMENSIONAL ARRAY TO JSON


   <?php
   $products = array
     (
     array("fruits",10),
     array("vegitables",15),
     array("fast food",5)
     );
   $js=json_encode($products);
   echo $js."\n";
   ?>
   

OUTPUT FOR MULTIDIMENSIONAL ARRAY TO JSON

[ ["fruits",10] , ["vegitables",15] , ["fast food",5] ]

LET'S CONVERT JSON TO ARRAY


     <?php
     $products = '{"name":"rahul","age":37,"salary":43000}';
     $js= json_decode($products,true);
     var_dump($js);
     ?>
     

OUTPUT FOR JSON TO ARRAY

array(3) { ["name"]=> string(5) "rahul" ["age"]=> int(37) ["salary"]=> int(43000) }

LET'S CONVERT ARRAY OF JSON OBJECTS TO ARRAY


      <?php
      $products = '[{"name":"rahul","age":37,"salary":43000},{"name":"prem","age":30,"salary":40000}]';
      $js= json_decode($products,true);
      var_dump($js);
      ?>
      

OUTPUT FOR ARRAY OF JSON OBJECTS TO ARRAY

array(2)
{
[0]=> array(3) {
["name"]=> string(5) "rahul"
["age"]=> int(37)
["salary"]=> int(43000)
}
[1]=> array(3) {
["name"]=> string(4) "prem"
["age"]=> int(30)
["salary"]=> int(40000)
}
}

This is the end of the session.Ihope you have liked it.Thank you for following this article.

About author
Supreet Patil
I am a web developer.I have started the journey of web development 2017.I am CSE degree holder.
E-mail:psupreet12@gmail.com
Leave a reply