json_encode() function is an inbuilt PHP function which is used to convert PHP array or object into JSON representation. Encoding is used to bundle data with respect to a particular format. This process will be required to preserve data consistency.
Convert array to JSON:
<?php
$input_ary = array();
$input_ary['array_key_1'] = 'array_value_1';
$input_ary['array_key_2'] = 'array_value_2';
$input_ary['array_key_3'] = 'array_value_3';
$input_ary['array_key_4'] = 'array_value_4';
$input_ary['array_key_5'] = 'array_value_5';
$json_result = json_encode($input_ary);
echo $json_result;
?>
Result:
{"array_key_1":"array_value_1","array_key_2":"array_value_2","array_key_3":"array_value_3",
"array_key_4":"array_value_4","array_key_5":"array_value_5"}
Convert Object to JSON – new stdClass():
<?php
$input_object = new stdClass();
$input_object->array_key_1 = 'array_value_1';
$input_object->array_key_2 = 'array_value_2';
$input_object->array_key_3 = 'array_value_3';
$input_object->array_key_4 = 'array_value_4';
$input_object->array_key_5 = 'array_value_5';
$json_result = json_encode($input_object);
echo $json_result;
?>
Result:
{"array_key_1":"array_value_1","array_key_2":"array_value_2","array_key_3":"array_value_3",
"array_key_4":"array_value_4","array_key_5":"array_value_5"}
Convert Object to JSON – (object)array():
<?php
$input_object = (object)array();
$input_object->array_key_1 = 'array_value_1';
$input_object->array_key_2 = 'array_value_2';
$input_object->array_key_3 = 'array_value_3';
$input_object->array_key_4 = 'array_value_4';
$input_object->array_key_5 = 'array_value_5';
$json_result = json_encode($input_object);
echo $json_result;
?>
Result:
{"array_key_1":"array_value_1","array_key_2":"array_value_2","array_key_3":"array_value_3",
"array_key_4":"array_value_4","array_key_5":"array_value_5"}

Related Posts
-
PHP: Convert JSON String to Array / Object using json_decodeHow to convert JSON String to Array / Object using json_decode? json_decode is inbuilt function in PHP. It will take a JSON string and converts it into a PHP array or object depends on arguments. Live demo: https://www.gtechhub.com/json-decode-online Convert JSON String to PHP Object: <?php $json_input = '{"array_key_1":"array_value_1","array_key_2":"array_value_2", "array_key_3":"array_value_3","array_key_4":"array_value_4","array_key_5":"array_value_5"}'; $json_result…
Get the first key of an array in PHP - array_key_firstGet the first key of an array without affecting the internal sequential array pointer. The array_key_first( ) function returns the first key of the array if the array is not empty, otherwise null. array_key_first() PHP Function available in PHP version 7 >= PHP version 7.3.0 Syntax of array_key_first PHP function array_key_first (…
-
PHP: Create empty object - new stdClass,var_dumpstdClass is the default PHP object. stdClass has no properties, methods or parent. It does not support magic methods, and implements no interfaces. Standard way to create an "empty" object is: <?php $input_object = new stdClass(); var_dump($input_object); ?> Result for new stdClass(): object(stdClass)#1 (0) { } But, with PHP Version…
-
Generating Random String Using PHPGenerating random strings with custom specified length is most required functionality in every project. In this article, I described the easiest way to generate custom length random string. PHP program for Random String Generate: <?php /* * Function to generate random string * Default length 30 character */ function generateRandomString($length…
Get the last element of an array in PHP – end()The end() function is an inbuilt function in PHP Programming. The purpose of this function is to find the last element of the given input array. The end() function changes the internal pointer of an array to the last element and returns the value of the last element. end() PHP…
-
How to add and subtracting days using php date functionPHP date() function formats a local date and time, and returns the formatted date string. In this article, I described the simple process about how to add and subtract number of days from current date using default PHP date() function. Add number of days in current date: php strtotime add…
How to add and subtracting weeks using php date() functionPHP date() function formats a local date and time, and returns the formatted date string. In this article, I described the simple process about how to add and subtract number of weeks from current date using default PHP date() function. Add number of weeks in current date: (Example: adding 5 weeks) <?php…
-
PHP: How to copy all files from one directory to anotherI will guide you to copy all files & sub directories of the directory to another directory using PHP copy() function. There are many other functions used to copy the content of one directory to another. copy() Functionopendir() Functionis_dir() Functionscandir( ) Functionreaddir() Function PHP code for copy multiple files from…
PHP Cheat SheetPHP code is usually processed on a web server by a PHP interpreter implemented as a module, a daemon or as a Common Gateway Interface (CGI) executable. On a web server, the result of the interpreted and executed PHP code — which may be any type of data, such as…