How to Convert Byte Array to JSON in PHP ? Last Updated : 05 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a Byte Array, the task is to convert Byte Array into JSON using PHP. Converting a byte array to JSON in PHP is a common task, especially when dealing with binary data or when you want to represent raw data in a JSON format. Table of Content Using base64_encode() and json_encode() FunctionsUsing Custom Conversion FunctionApproach 1: Using base64_encode() and json_encode() FunctionsOne common approach is to encode the byte array as a base64 string and then include it in a JSON structure. This method ensures that binary data is properly represented in a JSON-compatible format. PHP <?php // Byte Array $byteArray = [0x71, 0x101, 0x101, 0x107, 0x115]; // Convert byte array to base64 $base64String = base64_encode(pack('C*', ...$byteArray)); // Create a JSON structure $jsonData = json_encode(['data' => $base64String]); // Display the result echo $jsonData; ?> Output{"data":"cQEBBxU="} Approach 2: Using Custom Conversion FunctionYou can create a custom function to convert a byte array to a hexadecimal or binary string and then include it in a JSON structure. PHP <?php // Byte Array $byteArray = [71, 101, 101, 107, 115]; // Custom function to convert byte // array to hexadecimal string function byteArrayToHex($byteArray) { return implode('', array_map('dechex', $byteArray)); } // Convert byte array to hexadecimal $hexString = byteArrayToHex($byteArray); // Create a JSON structure $jsonData = json_encode(['data' => $hexString]); // Display the result echo $jsonData; ?> Output{"data":"4765656b73"} Comment More infoAdvertise with us Next Article How to convert an array to CSV file in PHP ? B blalverma92 Follow Improve Article Tags : PHP Geeks Premier League 2023 Similar Reads How to Encode Array in JSON PHP ? Encoding arrays into JSON format is a common task in PHP, especially when building APIs or handling AJAX requests. Below are the approaches to encode arrays into JSON using PHP: Table of Content Using json_encode()Encoding Associative ArraysCustom JSON SerializationUsing json_encode()PHP provides a 2 min read How to Convert JSON to ArrayBuffer in JavaScript? An ArrayBuffer is a complex data type, and structures which has a fixed length and takes binary content as the whole. Any variable that contains pure binary data will be defined in JavaScript as Simple Data, however on some occasions it is sometimes necessary to convert JSON data to an ArrayBuffer, 4 min read Convert Bytearray To Bytes In Python In Python, dealing with binary data is a common task, and understanding how to convert between different binary representations is crucial. One such conversion is from a bytearray to bytes. In this article, we will explore five simple methods to achieve this conversion, along with code examples for 3 min read How to convert an array to CSV file in PHP ? To convert an array into a CSV file we can use fputcsv() function. The fputcsv() function is used to format a line as CSV (comma separated values) file and writes it to an open file. The file which has to be read and the fields are sent as parameters to the fputcsv() function and it returns the leng 2 min read How to JSON Decode in PHP? JSON Decode is the process of converting a JSON string into a PHP variable, typically an object or an associative array. Then we will print that decode JSON text. Below are the approaches to JSON decoding in PHP: Table of Content Using json_decode() methodUsing Explode and Foreach LoopUsing json_dec 2 min read How to generate Json File in PHP ? In this article, we are going to generate a JSON file in PHP by using an array. JSON stands for JavaScript object notation, which is used for storing and exchanging data. JSON is text, written with JavaScript object notation. Structure: {"data":[ { "sub_data1":"value1", "sub_data2":"value2","sub_dat 3 min read Like