Las matrices PHP son correlaciones asociativas, en las que la clave puede ser un entero o una serie.
Las claves de enteros se pueden asignar automáticamente en orden ascendente secuencial, para trabajar de la misma manera que una matriz indexada con enteros tradicional. PHP da soporte a un operador de índice vacío ([]) que se puede utilizar en el lado izquierdo de una asignación para agregar un elemento a la matriz, utilizando un índice entero con el valor más alto del índice más 1.
Las matrices PHP asociativas se modelan como estructuras de objetos JSON, porque las matrices JSON son anónimas.
El nodo PHPCompute da soporte a la constante PHP MB_JSON_ARRAY, que puede utilizar en el método setType() de MbElement para cambiar un elemento de objeto JSON existente en una matriz JSON. Este método se utiliza generalmente cuando los elementos se han creado copiándolos de un árbol propiedad de otro dominio de analizador.
Para acceder o cambiar un valor existente de un elemento de matriz JSON en un árbol, puede utilizar un índice de matriz ([index]) para seleccionar el elemento necesario. Los elementos de la matriz JSON no se pueden crear en el árbol de JSON utilizando el formato [index]; intentar hacerlo dará como resultado una excepción.
->address['street']
Los ejemplos siguientes muestran cómo puede utilizar matrices PHP con JSON:
$strVar = "this";
$boolVar = true;
$output_assembly->JSON->Data[] = $strVar;
$output_assembly->JSON->Data[] = $boolVar;
Debe utilizar el operador de índice de agregar matriz ([]) para agregar a una matriz; no puede utilizar el formato [index].
(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)
)
)
Esta es una estructura de datos PHP no válida porque tiene una clave duplicada, que no puede existir en una matriz asociativa. Como resultado, no se puede acceder, crear ni modificar esta forma de datos JSON desde un nodo PHPCompute. Para procesar este tipo de datos JSON, el flujo de mensajes debe utilizar ESQL o Java™.