See information about the latest product version
Using PHP arrays with JSON
PHP arrays are associative maps, in which the key can be an integer or a string.
Integer keys can be allocated automatically in ascending sequential order, to work in the same way as a traditional integer indexed array. PHP supports an empty index operator ([]), which can be used on the left side of an assignment to add an item to the array, using an integer index with the value of the highest current index plus 1.
PHP associative arrays are modeled as JSON object structures, because JSON arrays are anonymous.
- A JSON array is produced by assigning a contiguous integer-indexed PHP array into a JSON domain tree path. A PHP array variable is contiguous integer-indexed if it contains only integer keys that are sequenced from 0 to n-1 (where n is the total number of items in the array). The tree elements have their type set automatically to JSON.Array, which produces a JSON array format in the bit stream when the tree is serialized.
- A JSON object is produced by assigning an associative PHP array into a JSON domain tree path. A PHP array is associative if it contains one or more string keys, or if it has integer keys that are not contiguously sequenced from 0 to n-1.
- To create a JSON array and add the first item into it
- To append an item to the end of an existing JSON array
The PHPCompute node supports the PHP constant MB_JSON_ARRAY, which you can use in the MbElement setType() method to change an existing JSON object element into a JSON array. This method is typically used when elements have been created by copying from a tree owned by another parser domain.
To access or change an existing value of a JSON array item in a tree, you can use an array index ([index]) to select the required item. JSON array items cannot be created in the JSON tree by using the [index] form; attempts to do so result in an exception being thrown.
->address['street']
The following examples demonstrate how you can use PHP arrays with JSON:
- Creating a JSON array message from individual PHP variables
- Creating a JSON array message from a PHP indexed array
- Creating a JSON object message from a PHP associative array
- Creating a JSON message from a PHP integer index multidimensional array
- Creating a JSON message from a PHP mixed associative and integer indexed multidimensional array
- Updating a JSON array in the logical tree
- Copying an XML domain message subtree with repeating elements to a JSON domain tree and converting it to a JSON array using PHP
- Working with a JSON object with a repeating name
Creating a JSON array message from individual PHP variables
$strVar = "this";
$boolVar = true;
$output_assembly->JSON->Data[] = $strVar;
$output_assembly->JSON->Data[] = $boolVar;
You must use the array append index operator ([]) to append to an array; you cannot use the [index] form.
(Message):JSON = ( ['json' : 0xhhhhhh]
(0x01001000:Array ): Data = (
(0x03000000:NameValue): Item = 'this' (CHARACTER)
(0x03000000:NameValue): Item = TRUE (BOOLEAN)
)
)
["this",true]
$output_assembly->JSON->Data[][] = '00';
$data = $output_assembly->JSON->Data;
$data[0][] = '01';
$data[][] = '10';
$data[1][] = '11';
[["00","01"],["10","11"]]
Creating a JSON array message from a PHP indexed array
$arr = array("this", "that");
$output_assembly->JSON->Data = $arr;
(Message):JSON = ( ['json' : 0xhhhhhh]
(0x01001000:Array ): Data = (
(0x03000000:NameValue): 0 = 'this' (CHARACTER)
(0x03000000:NameValue): 1 = 'that' (CHARACTER)
)
)
["this", "that" ]
Creating a JSON object message from a PHP associative array
$arr = array('first' => "1st", 'second' => "2nd");
$output_assembly->JSON->Data = $arr;
(Message):JSON = ( ['json' : 0xhhhhhh]
(0x01000000:Object ): Data = (
(0x03000000:NameValue): first = '1st' (CHARACTER)
(0x03000000:NameValue): second = '2nd' (CHARACTER)
)
)
$var1 = $output_assembly->JSON->Data['second']; // 2nd
$var2 = $output_assembly->JSON->Data['none']; // null
{"first":"1st","second":"2nd"}
Creating a JSON message from a PHP integer index multidimensional array
$arr = array(array("a1","a2"),array("b1","b2"));
$output_assembly->JSON->Data = $arr;
(Message):JSON = ( ['json' : 0xhhhhhh]
(0x01001000:Array ): Data = (
(0x01001000:Array ): 1 = (
(0x03000000:NameValue): 1 = 'a1' (CHARACTER)
(0x03000000:NameValue): 2 = 'a2' (CHARACTER)
)
(0x01001000:Array ): 2 = (
(0x03000000:NameValue): 1 = 'b1' (CHARACTER)
(0x03000000:NameValue): 2 = 'b2' (CHARACTER)
)
)
)
[["a1","a2"],["b1","b2"]]
Creating a JSON message from a PHP mixed associative and integer indexed multidimensional array
$arr = array("top1" => array("a1","a2"), "top2" => array("b1","b2"));
$output_assembly->JSON->Data = $arr;
(Message):JSON = ( ['json' : 0xhhhhhh]
(0x01000000:Object ): Data = (
(0x01001000:Array ):top1 = (
(0x03000000:NameValue): 1 = 'a1' (CHARACTER)
(0x03000000:NameValue): 2 = 'a2' (CHARACTER)
)
(0x01001000:Array ):top2 = (
(0x03000000:NameValue): 1 = 'b1' (CHARACTER)
(0x03000000:NameValue): 2 = 'b2' (CHARACTER)
)
)
)
{"top1":["a1","a2"],"top2":["b1","b2"]}
Updating a JSON array in the logical tree
[["a1","a2"],["b1","b2"]]
(Message):JSON = ( ['json' : 0xhhhhhh]
(0x01001000:Array ): Data = (
(0x01001000:Array ): Item = (
(0x03000000:NameValue): Item = 'a1' (CHARACTER)
(0x03000000:NameValue): Item = 'a2' (CHARACTER)
)
(0x01001000:Array ): Item = (
(0x03000000:NameValue): Item = 'b1' (CHARACTER)
(0x03000000:NameValue): Item = 'b2' (CHARACTER)
)
)
)
output_assembly->JSON->Data[0][0] = "A1";
output_assembly->JSON->Data[1][1] = "B2";
output_assembly->JSON->Data[1][] = "New3";
[["A1","a2"],["b1","B2","new3"]]
Copying an XML domain message subtree with repeating elements to a JSON domain tree and converting it to a JSON array using PHP
<doc>
<cats>
<cat>thing1</cat>
<cat>thing2</cat>
</cats>
</doc>
(0x01000000:Folder):XMLNSC = ( ['xmlnsc' : 0xhhhhhh]
(0x01001000:Folder ): doc = (
(0x01001000:Folder ): cats = (
(0x03000000:NameValue): cat = 'thing1' (CHARACTER)
(0x03000000:NameValue): cat = 'thing2' (CHARACTER)
)
)
)
{"cats":["thing1","thing2"]}
$output_assembly->JSON->Data = $input_assembly->XMLNSC->doc;
$output_assembly->JSON->Data->cats->setType(MB_JSON_ARRAY);
(Message):JSON = ( ['json' : 0xhhhhhh]
(0x01000000:Obejct):Data = (
(0x01001000:Array ): cats = (
(0x03000000:NameValue): cat = 'thing1' (CHARACTER)
(0x03000000:NameValue): cat = 'thing2' (CHARACTER)
)
)
)
Working with a JSON object with a repeating name
{"cat":"thing1","cat":"thing2" }
(Message):JSON = ( ['json' : 0xhhhhhh]
(0x01001000:Object ): Data = (
(0x03000000:NameValue): cat = 'thing1' (CHARACTER)
(0x03000000:NameValue): cat = 'thing2' (CHARACTER)
)
)
This is an invalid PHP data structure because it has a duplicate key, which cannot exist in an associative array. As a result, this form of JSON data cannot be accessed, created, or modified from a PHPCompute node. To process this type of JSON data, the message flow must use either ESQL or Java™.