PHP Extension
PHP Extension
Merge SP-API JSON Order Arrays
See more Amazon SP-API Examples
Demonstrates how to merge JSON arrays for the case of Amazon SP-API orders.Chilkat PHP Extension Downloads
<?php
include("chilkat.php");
$success = false;
// Batch 1 File
$a1 = '{\'payload\': {\'Orders\': [{\'AmazonOrderId\': \'1\',\'OrderStatus\': \'Unshipped\'},{\'AmazonOrderId\': \'2\',\'OrderStatus\': \'Unshipped\'}]}';
// Batch 2 File
$a2 = '{\'payload\': {\'Orders\': [{\'AmazonOrderId\': \'3\',\'OrderStatus\': \'Unshipped\'},{\'AmazonOrderId\': \'4\',\'OrderStatus\': \'Unshipped\'}]}';
// Required Merged File
// {"payload": {"Orders": [{"AmazonOrderId": "1","OrderStatus": "Unshipped"},{"AmazonOrderId": "2","OrderStatus": "Unshipped"},{"AmazonOrderId": "3","OrderStatus": "Unshipped"},{"AmazonOrderId": "4","OrderStatus": "Unshipped"}]}
$success = true;
$json1 = new CkJsonObject();
$json1->Load($a1);
$json2 = new CkJsonObject();
$json2->Load($a2);
// We're going to add the order records from json2 to json1.
// jarr1 is a CkJsonArray
$jarr1 = $json1->ArrayOf('payload.Orders');
// jarr2 is a CkJsonArray
$jarr2 = $json2->ArrayOf('payload.Orders');
$i = 0;
$numOrders = $jarr2->get_Size();
while (($i < $numOrders)) {
// jOrder is a CkJsonObject
$jOrder = $jarr2->ObjectAt($i);
// Assuming non-null return...
$jarr1->AddObjectCopyAt(-1,$jOrder);
$i = $i + 1;
}
// Show the merged JSON.
$json1->put_EmitCompact(false);
print $json1->emit() . "\n";
// Result:
// {
// "payload": {
// "Orders": [
// {
// "AmazonOrderId": "1",
// "OrderStatus": "Unshipped"
// },
// {
// "AmazonOrderId": "2",
// "OrderStatus": "Unshipped"
// },
// {
// "AmazonOrderId": "3",
// "OrderStatus": "Unshipped"
// },
// {
// "AmazonOrderId": "4",
// "OrderStatus": "Unshipped"
// }
// ]
// }
// }
?>