Author
This package can encode large arrays to JSON using less memory.
It can output JSON-encoded string for an array by adding each array element immediately.
The JSON-encoded string is outputted as part of the current PHP script output without storing the whole array of elements as a variable, thus avoiding taking too much memory.
PHP JSON Encode large data with lesser resources
<?php
require_once __DIR__ . '/AutoloadJsonEncode.php';
use CustomJsonEncode\JsonEncoder;
// Create JsonEncode Object.
$jsonEncodeObj = JsonEncoder::getObject();
// Execute DB Query
$stmt = $db->select($sql);
$stmt->execute($params);
// For single row - one
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$jsonEncode->encode($row);
// For single row - two
$jsonEncode->startObject();
foreach($stmt->fetch(PDO::FETCH_ASSOC) as $key => $value) {
$jsonEncode->addKeyValue($key, $value);
}
$jsonEncode->endObject();
// Free statement resources and close the cursor.
$stmt->closeCursor();
$jsonEncode = null;
<?php
require_once __DIR__ . '/AutoloadJsonEncode.php';
use CustomJsonEncode\JsonEncoder;
// Create JsonEncode Object.
$jsonEncodeObj = JsonEncoder::getObject();
// Execute DB Query
$stmt = $db->select($sql);
$stmt->execute($params);
// For multiple rows
$jsonEncode->startArray();
for(;$row=$stmt->fetch(PDO::FETCH_ASSOC);) {
$jsonEncode->encode($row);
}
$jsonEncode->endArray();
// Free statement resources and close the cursor.
$stmt->closeCursor();
$jsonEncode = null;
<?php
require_once __DIR__ . '/AutoloadJsonEncode.php';
use CustomJsonEncode\JsonEncoder;
// Create JsonEncode Object.
$jsonEncodeObj = JsonEncoder::getObject();
// Start JSON object
$jsonEncode->startObject();
// Execute DB Query - 1
$stmt = $db->select($sql);
$stmt->execute($params);
foreach($stmt->fetch(PDO::FETCH_ASSOC) as $key => $value) {
$jsonEncode->addKeyValue($key, $value);
}
// Free statement resources and close the cursor.
$stmt->closeCursor();
// Execute DB Query - 2 (which returns single row)
$stmt = $db->select($sql_2);
$stmt->execute($params_2);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt->closeCursor();
// Append key / value pair (value can be an integer / string / array)
$jsonEncode->addKeyValue('subCatgories', $row);
// End JSON object
$jsonEncode->endObject();
$jsonEncode = null;
<?php
require_once __DIR__ . '/AutoloadJsonEncode.php';
use CustomJsonEncode\JsonEncoder;
// Create JsonEncode Object.
$jsonEncodeObj = JsonEncoder::getObject();
// Start JSON object
$jsonEncode->startObject();
// Execute DB Query - 1
$stmt = $db->select($sql);
$stmt->execute($params);
foreach($stmt->fetch(PDO::FETCH_ASSOC) as $key => $value) {
$jsonEncode->addKeyValue($key, $value);
}
// Free statement resources and close the cursor.
$stmt->closeCursor();
// Start JSON array inside object
$objectKey = 'subCatgories';
$jsonEncode->startArray($objectKey);
// Execute DB Query - 2
$stmt = $db->select($sql_2);
$stmt->execute($params_2);
for(; $row=$stmt->fetch(PDO::FETCH_ASSOC);) {
$jsonEncode->encode($row);
}
// Free statement resources and close the cursor.
$stmt->closeCursor();
// End JSON array inside object
$jsonEncode->endArray();
// End JSON object
$jsonEncode->endObject();
$jsonEncode = null;
<?php
require_once __DIR__ . '/AutoloadJsonEncode.php';
use CustomJsonEncode\JsonEncoder;
// Create JsonEncode Object.
$jsonEncodeObj = JsonEncoder::getObject();
// Start JSON object
$jsonEncode->startObject();
// Execute DB Query - 1
$stmt = $db->select($sql);
$stmt->execute($params);
foreach($stmt->fetch(PDO::FETCH_ASSOC) as $key => $value) {
$jsonEncode->addKeyValue($key, $value);
}
// Free statement resources and close the cursor.
$stmt->closeCursor();
// Append Json
$jsonEncode->appendJson('{"NewKey1": {"Key11": "Value11", "Key12": "Value12"}}');
// Append Json for a dynamic Key
$jsonEncode->appendKeyJson("NewKey2", '{"Key21": "Value21", "Key22": "Value22"}');
// End JSON object
$jsonEncode->endObject();
$jsonEncode = null;
> The $jsonEncode = null; will stream the generated JSON.
File | Role | Description | ||
---|---|---|---|---|
Files folder imageCustomJsonEncode (3 files) | ||||
Plain text file Autoload.php | Class | Class source | ||
Accessible without login Plain text file AutoloadJsonEncode.php | Aux. | Configuration script | ||
Accessible without login Plain text file Example.php | Example | Example script | ||
Accessible without login Plain text file LICENSE | Lic. | License text | ||
Accessible without login Plain text file README.md | Example | Example script |
File | Role | Description |
---|---|---|
Plain text file JsonEncode.php | Class | Class source |
Plain text file JsonEncodeObject.php | Class | Class source |
Plain text file JsonEncoder.php | Class | Class source |
Add link image If you know an application of this package, send a message to the author to add a link here.