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.
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:
$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"]]
$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" ]
$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"}
$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"]]
$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"]}
[["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"]]
<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)
)
)
)
{"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™.