BOOLEANS

php logo

Before discussing Arrays let’s do a brief review of Booleans. The Boolean type is a simple but important data type. They allow us to perform operations or not based on the Boolean value.

Boolean

INTRODUCTION TO ARRAYS

So far we have analyzed simple or primitive data types. In PHP we can define compound values through arrays.

ARRAY ORDERED

$alumni = [‘Michele’,’Silvia’,’Luca‘];It takes the name of sorted or numbered array because each element of the array is associated with a numeric index starting with 0.

SORTED ARRAY CODE

Copy to Clipboard

ADD AND REMOVE CODE ELEMENTS

Copy to Clipboard

ARRAY UNPACKING CODE

Copy to Clipboard

MULTIDIMENSIONAL ARRAYS CODE

Copy to Clipboard

TYPE CASTING CODE

Copy to Clipboard

DEEPENING

An array in PHP is a data structure that allows multiple values to be stored in a single variable. Arrays in PHP can contain elements of different data types, such as strings, integers, objects, and even other arrays. There are three main types of arrays in PHP:

1. Indexed (or numeric) arrays: Indexed arrays use a numeric index to access stored values. Indexes start at 0 for the first element and increase by 1 for each subsequent element.

$frutti = array(“mela”, “banana”, “arancia”);
echo $frutti[0]; // Restituisce “mela”2.

2. Associative arrays: Associative arrays use keys, which are strings or numbers, to access stored values. This type of array is particularly useful when you want to associate values with specific names.

$età = array(“Mario” => 30, “Luigi” => 28, “Peach” => 24);
echo $età[“Mario”]; // Restituisce 30

3. Multidimensional Arrays: Multidimensional arrays are arrays that contain other arrays as elements. They can have more than two dimensions and are useful for representing more complex data structures, such as arrays or tables.

$persone = array(
Mario” => array(“età” => 30, “città” => “Roma“),
Luigi” => array(“età” => 28, “città” => “Milano“),
Peach” => array(“età” => 24, “città” => “Venezia“)
);
echo $persone[“Luigi“][“città“]; // Restituisce “Milano”

Arrays in PHP are extremely versatile and powerful, and are used in many different contexts to handle structured data. PHP offers a wide range of built-in functions for working with arrays, such as counting elements (count()), adding new elements (array_push()), removing elements (unset()), sorting (sort() and asort()), and many others.

LINKS TO PREVIOUS POSTS

THE PHP LANGUAGE

LINK TO THE CODE ON GITHUB

GITHUB