Recommend this page to a friend! |
Download |
Info | Example | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
2024-10-16 (20 hours ago) | Not enough user ratings | Total: 208 | All time: 8,393 This week: 36 |
Version | License | PHP version | Categories | |||
heavyjsonencode 2.0.4 | MIT/X Consortium ... | 5 | PHP 5, Data types |
PHP JSON Encode large data with lesser resources
<?php
require "JsonEncode.php";
// 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 "JsonEncode.php";
// 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 "JsonEncode.php";
// 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 "JsonEncode.php";
// 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;
> The $jsonEncode = null; will stream the generated JSON.
Files (3) |
File | Role | Description |
---|---|---|
JsonEncode.php | Class | Encode Array to JSON |
LICENSE | Lic. | License text |
README.md | Example | Example script |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.