Associative Array
Associative Array
A PL/SQL associative array is a collection type that associates a unique key with
a value. An associative array has the following characteristics: An associative array
type must be defined before array variables of that array type can be declared. Data
manipulation occurs in the array variable.
A PL/SQL associative array is a collection type that associates a unique key with a
value.
An associative array type must be defined before array variables of that array type can be
declared. Data manipulation occurs in the array variable.
The array does not need to be initialized; simply assign values to array elements.
There is no defined limit on the number of elements in the array; it grows dynamically as
elements are added.
The array can be sparse; there can be gaps in the assignment of values to keys.
An attempt to reference an array element that has not been assigned a value results in an
exception.
Syntax:
TYPE__assoctype___IS TABLE
OF___datatype___INDEX
BY___BINARY_INTEGERPLS___INTEGERVARCHAR
2___(___n___BYTE___CHAR)
Description:
TYPE assoctype
Specifies an identifer for the array type.
datatype
Specifies a supported data type, such as VARCHAR2, NUMBER, RECORD, VARRAY, or
associative array type. The %TYPE attribute and the %ROWTYPE attribute are also
supported.
INDEX BY
Specifies that the associative array is to be indexed by one of the data types introduced by
this clause.
BINARY INTEGER
Integer numeric data.
PLS_INTEGER
Integer numeric data.
VARCHAR2 (n[BYTE|CHAR])
A variable-length character string of maximum length n code units, which may range from
1 to 32 672 BYTE or from 1 to 8 168 CHAR. The %TYPE attribute is also supported if the
object to which the %TYPE attribute is being applied is of the BINARY_INTEGER,
PLS_INTEGER, or VARCHAR2 data type.
Index-By Table
An index-by table (also called an associative array) is a set of key-value pairs.
Each key is unique and is used to locate the corresponding value. The key can be
either an integer or a string.
An index-by table is created using the following syntax. Here, we are creating
an index-by table named table_name, the keys of which will be of the
subscript_type and associated values will be of the element_type
TYPE type_name IS TABLE OF element_type [NOT NULL] INDEX BY subscript_type;
table_name type_name;
TYPE associative_array_type
IS TABLE OF datatype [NOT NULL]
INDEX BY index_type;
In this syntax:
associative_array associative_array_type
Code language: SQL (Structured Query Language) (sql)
t_capital t_capital_type;
Code language: SQL (Structured Query Language) (sql)
array_name(index)
Code language: SQL (Structured Query Language) (sql)
array_name(index) := value;
Example:
Here array() function is used to create associative array.
<?php
/* First method to create an associate array. */
"Chemistry"=>96, "English"=>93,
"Computer"=>98);
$student_two["Maths"] = 95;
$student_two["Physics"] = 90;
$student_two["Chemistry"] = 96;
$student_two["English"] = 93;
$student_two["Computer"] = 98;
?>
Output: