PHP JSON Array tutorial with Example

PHP and JSON

PHP helps to handle JSON has some built-in functions. 

php json array

PHP allow us to JSON encode and decode help of this two functions:

  • Json_encode()
  • Json_decode()

PHP Array to JSON - Json_encode


Json_encode()  the function is to return the encoded value to JSON format.

Syntax:

Json_encode(mixed $value,int $flags,int $depth= 512)

value − The value being encoded UTF-8 data.

flags JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT.


  
<?php
//associative array into a JSON object
$age = array("chandra"=>28, "roshan"=>13, "priya"=>24);

echo json_encode($age);
?>
  
  

Output:

 
{"chandra":28,"roshan":13,"priya":24}
  
  



  
<?php
   class student {
      public $firstname = "";
      public $class  = "";
      public $age = "";
   }
	
   $s = new student();
   $s->firstname = "chandra";
   $s->class  = "6";
   $s->age ="15";


   echo json_encode($s);
?>
  
  

Output:

  
{"firstname":"chandra","class":"6","age":"15"}
  
  

Some using Flags Examples: 

JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT
  
<?php

$student = array('<chandra>',"'6'",'"15"','&17-06-1998&', "\xc3\xa9");

echo "Normal: ",  json_encode($student), "\n";
echo "Tags: ",    json_encode($student, JSON_HEX_TAG), "\n";
echo "Apos: ",    json_encode($student, JSON_HEX_APOS), "\n";
echo "Quot: ",    json_encode($student, JSON_HEX_QUOT), "\n";
echo "Amp: ",     json_encode($student, JSON_HEX_AMP), "\n";
echo "Unicode: ", json_encode($student, JSON_HEX_TAG | JSON_UNESCAPED_UNICODE), "\n";
echo "All: ",     json_encode($student, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE), "\n\n";

?>
  
  


Output:

  
Normal: ["","'6'","\"15\"","&17-06-1998&","\u00e9"] 
Tags: ["\u003Cchandra\u003E","'6'","\"15\"","&17-06-1998&","\u00e9"] 
Apos: ["","\u00276\u0027","\"15\"","&17-06-1998&","\u00e9"] 
Quot: ["","'6'","\u002215\u0022","&17-06-1998&","\u00e9"]
Amp: ["","'6'","\"15\"","\u002617-06-1998\u0026","\u00e9"] 
Unicode: ["\u003Cchandra\u003E","'6'","\"15\"","&17-06-1998&","é"] 
All: ["\u003Cchandra\u003E","\u00276\u0027","\u002215\u0022","\u002617-06-1998\u0026","é"]
  
  



  
<?php
$empty_array = array();

echo "Empty array output: ", json_encode($empty_array), "\n";
echo "Empty array output as object: ", json_encode($empty_array, JSON_FORCE_OBJECT), "\n\n";

$nonass = array(array(121,222,333));

echo "Non-associative array output: ", json_encode($nonass), "\n";
echo "Non-associative array output as object: ", json_encode($nonass, JSON_FORCE_OBJECT), "\n\n";

$assarr = array('name' => 'chandra', 'age' => '28');

echo "Associative array output: ", json_encode($assarr), "\n";
echo "Associative array output as object: ", json_encode($assarr, JSON_FORCE_OBJECT), "\n\n";
?>
  
  

Output:

  
Empty array output: [] 
Empty array output as object: {} 
Non-associative array output: [[121,222,333]] 
Non-associative array output as object: {"0":{"0":121,"1":222,"2":333}} 
Associative array output: {"name":"chandra","age":"28"} 
Associative array output as object: {"name":"chandra","age":"28"} 
  
  



JSON_HEX_TAG - It's converted < and > all into \u003C and \u003E. 
JSON_HEX_AMP - It's converted & all into \u0026. 
JSON_HEX_APOS - It's converted ' all into \u0027. 
JSON_HEX_QUOT - It's converted " all into \u0022. 
JSON_FORCE_OBJECT - outputs an object array when a non-associative array is used. It also displays an array as an empty object. 
JSON_NUMERIC_CHECK - Encodes numeric strings as numbers. 
JSON_UNESCAPED_UNICODE - It's encoded multibyte Unicode characters.

PHP - Json_decode To Array 


 The json_decode() the function is convert a json object to php object.
  
<?php
//json_decode function
$age = '{"chandra":28, "roshan":13, "priya":24}';

$jsonobj=json_decode($age,true);
echo $jsonobj["chandra"];
echo $jsonobj["roshan"] ;
echo $jsonobj["priya"]; 
?>
  
  

Output:

  
28 13 24
  
  



  
<?php
//json_decode function
$age = '{"chandra":28, "roshan":13, "priya":24}';

$jsonobj=json_decode($age);
echo $jsonobj->chandra;
echo $jsonobj->roshan;
echo $jsonobj->priya;
?>
  
  
Output:
  
28 13 24
  
  
  
<?php
//json_decode function
$age = '{"chandra":28, "roshan":13, "priya":24}';
var_dump(json_decode($age));
var_dump(json_decode($age,true));
?>
  
  

Output:

  
object(stdClass)#3 (3) { ["chandra"]=> int(28) ["roshan"]=> int(13) ["priya"]=> int(24) }

array(3) { ["chandra"]=> int(28) ["roshan"]=> int(13) ["priya"]=> int(24) }
  
  



Invalid JSON STRINGS Examples:


//single quotes are not valid 

$student = "{'name' : 'chandra' }"; 
json_decode($student); //return null 

$student = '{name : "chandra" }'; 
json_decode($student); //return null 

//commas are not allowed 

 $student = '{name : "chandra", }';
 json_decode($student); //return null

Some using Flags Examples:


JSON_BIGINT_AS_STRING - It decodes large integers. 
JSON_OBJECT_AS_ARRAY - It's decode JSON objects as a PHP array. 
JSON_THROW_ON_ERROR - The Json throws on error occurs instead of global error is retrieved with json_last_error().
  
<?php

$student = json_encode(
    array(
        1 => array(
            'chandra' => array(
                'english',
                'maths',
	            'social',
	            'science',
	            'evs'
            ),
            'priya' => array(
                'maths',
                'science'
            )
        )
    )
);

var_dump(json_decode($student, true, 4));
echo 'Last error: ', json_last_error_msg();



?>
  
  

Output:

  
array(1) { [1]=> array(2) { ["chandra"]=> array(5) { [0]=> string(7) "english" [1]=> string(5) "maths" [2]=> string(6) "social" [3]=> string(7) "science" [4]=> string(3) "evs" } ["priya"]=> array(2) { [0]=> string(5) "maths" [1]=> string(7) "science" } } }
Last error: No error
  
  



JSON_BIGINT

  
<?php
$json_bigint = '{"number": 1234567899874563210123456}';

var_dump(json_decode($json_bigint));
var_dump(json_decode($json_bigint, false, 512, JSON_BIGINT_AS_STRING));

?>
  
  


Output:

  
object(stdClass)#3 (1) { ["number"]=> float(1.2345678998745632E+24) } 
object(stdClass)#3 (1) { ["number"]=> string(25) "1234567899874563210123456" }
  
  




I hope you will learn PHP JSON array on two types encode and decode and text-based format and if you like please share and support us.
Previous Post Next Post