Array data structure

A

A string of data or an array is a structure that contains multiple values ​​of the same type of data. The length of a data string is set at the creation of the array and will be fixed over its entire existence. It is often convenient to store multiple values ​​in a variable. Such a variable is called a matrix, and the individual values ​​are called matrix elements.

How to declare an array?

Let us consider that a string is an object containing a set of elements. These elements can be either primitive (int, float, char), or a reference type (derived from Object). To declare a string of specific type data, specify the type of date followed by brackets:
int [] data_string;

Once a string is declared, it can be instantiated. It is necessary that before using the string, it is instantiated. Like any reference, use the new instantiation operator, followed by the data type and number of string elements:

int [] data_string = new int [10];

Here’s a function that uses the instance of a string of a given size as a parameter:
int [] createsString (int size) {return new int [size];}

If we try to create a string whose length is negative, then the NegativeArraySizeException error will be shown. However, the zero-length strings are syntactically correct.

An array (vector) represents a succession of elements in the form of key pairs -> value, where each key uniquely identifies an element. Unlike other languages ​​where the key should be numeric, in PHP the key can be both numeric and string, and array values ​​can have any data. In PHP there are 3 types of arrays:

1. Array Numbers – A numeric key array.

Values ​​are stored and accessed in a linear fashion.

2. Associative Arrays – A string array.

Values ​​are accessed by key and not in a linear fashion as above.

3. Multidimensional arrays:

An array that has one or more arrays.

1. Numerical Arrays

This type of arrays can store numbers, strings or objects, but their keys must be numeric. Normally the index starts at 0.
/ * A method of creating an array. * /
$ numbers = array (1, 2, 3, 4, 5);
for each ($ number as $ value) {
  echo “The value is $ value <br />”;
}
/ * The second method. * /
$ numbers [0] = “one”;
$ numbers [1] = “two”;
$ numbers [2] = “three”;
$ numbers [3] = “four”;
$ numbers [4] = “five”;

foreach ($ number as $ value) {
  echo “The value is $ value <br />”;
}

2. Associative Arrays

Associative arrays are similar to numeric arrays, the only difference being the index (key)
$ materials = array (
                “nails” => 3,
                “screws” => 1,
                “nuts” => 12
                )
echo “Material Required List:”;
foreach ($ buy as $ key -> $ value) {
  echo “$ key -“. $ value [$ key]. “<br />”;
}
// The code above is similar to this:
echo “Material Required List:”;
    echo $ shopping [‘nails’]. “<br/>”;
    echo $ shopping [‘screws’]. “<br/>”;
    echo $ shopping [‘nuts’]. “<br/>”;

3. Multidimensional arrays

A multidimensional array is an array that has one or more array values
$ favorite music = array (
            “pop” => array (
                        “Abba”
                        )
            “rock” => array (
                        “Queen”
                        )
            “symfonica” => array (
                        “Bach”
                        “Ravel”
                        “Beethoven”

The way of using multi-dimensional arrays is the same as that of simple ones.

Recent Posts

Archives

Categories