PHP: json_encode() Convert Array Into The Json Format: json_encode() convert any values into the json format, supported PHP version PHP 5 >= 5.2.0, PHP 7, PHP 8, PECL and json >= 1.2.0.
Parameters
Value: The value being encoded Can be any type except a resource, But All string data must be encoded in UTF-8.
Flag: Bitmask consisting of JSON_FORCE_OBJECT, JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_NUMERIC_CHECK, JSON_PARTIAL_OUTPUT_ON_ERROR, JSON_PRESERVE_ZERO_FRACTION, JSON_PRETTY_PRINT, JSON_UNESCAPED_LINE_TERMINATORS, JSON_UNESCAPED_SLASHES, JSON_UNESCAPED_UNICODE, JSON_THROW_ON_ERROR. if you want to learn more about the json_encode flag, please learn from here.
Depth: Set the maximum depth must be greater than zero(0).
Return Value: Return a json_encoded format string on success or false on failure.
json_encode() Code Example:
here we have made some code example based on php json_encode() function and Array, in these examples we have converted some Array into the json format.
Example 1:
<?php // we take some number value to encode into json format. $simpleArray = array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); echo json_encode( $simpleArray ); // output: [1,2,3,4,5,6,7,8,9,10] ?>
Example 2:
<?php // we take some string value to encode into json format. $simpleArray = array( 'hello', 'this', 'is', 'the', 'test', 'string' ); echo json_encode( $simpleArray ); // output: ["hello","this","is","the","test","string"] ?>
Example 3:
<?php // this example we will convering array into another way $simpleArray = [ 'hello', 'this', 'is', 'the', 'test', 'string', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]; echo json_encode( $simpleArray ); // output: ["hello","this","is","the","test","string",1,2,3,4,5,6,7,8,9,10] ?>
Example 4:
<?php // now we convert the array object to json object. $simpleArray = array( 'testNumberObject' => array( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 'testStringObject' => array( 'foo', 'bar', 'loop', 'control', 'function' ), ); echo json_encode( $simpleArray ); // output: {"testNumberObject":[1,2,3,4,5,6,7,8,9,10],"testStringObject":["foo","bar","loop","control","function"]} ?>
Example 5:
<?php // now we convert the multi array object to json object. $simpleArray = array( 'multiArray' => array( 'multiValue' => array( 1, 2, 3, 'hello', 'codeForest', 'learn', 'Code', 'anotherMultyArray' => array('this', 'is', 'the', 4, 'array')), ), ); echo json_encode( $simpleArray ); // output: {"multiArray":{"multiValue":{"0":1,"1":2,"2":3,"3":"hello","4":"codeForest","5":"learn","6":"Code","anotherMultyArray":["this","is","the",4,"array"]}}} ?>
Example 6:
<?php // if you want convert array into the json format for any action pages or ajax call then follow the simple steps. // first define the array. $arr = []; if(isset($_GET['action'])){ if(!empty($_GET['action']) && $_GET['action'] =='1'){ $arr['status'] = 'success'; $arr['message'] = 'action taken'; $arr['html'] = '<h1>Action has been taken</h1><a href="#">Visit Home</a>'; }else{ $arr['status'] = 'error'; $arr['message'] = 'missing required values!'; } }else{ $arr['status'] = 'error'; $arr['message'] = 'action missing'; } echo json_encode( $arr ); // output on success: {"status":"success","message":"action taken","html":"<h1>Action has been taken<\/h1><a href=\"#\">Visit Home<\/a>"} // output on error 1: {"status":"error","message":"action missing"} // output on error 2: {"status":"error","message":"missing required values!"} ?>
Still if you have any question please comment below, i will try to help you out!