Skip to content

Creating index based and associative array

In PHP, you can create both index-based arrays and associative arrays. Index-based arrays use numeric indexes to access elements, while associative arrays use custom keys (usually strings) to access elements. Here’s how you can create both types of arrays:

Index-Based Array:

To create an index-based array, you can use the array() constructor or square brackets []. Here’s an example:

// Using array() constructor

$indexArray = array(“apple”, “banana”, “cherry”);

 // Using square brackets (PHP 5.4+)

$indexArray = [“apple”, “banana”, “cherry”];

In an index-based array, elements are assigned numeric indexes starting from 0, 1, 2, and so on.

Associative Array:

To create an associative array, you assign custom keys to the elements. Here’s an example:

$assocArray = [ “first_name” => “John”, “last_name” => “Doe”, “age” => 30 ];

In an associative array, you can access elements using the custom keys you’ve assigned. For example, $assocArray[“first_name”] will give you the value “John.”

You can also create an associative array using the array() constructor and specifying key-value pairs:

$assocArray = array( “first_name” => “John”, “last_name” => “Doe”, “age” => 30 );

Both index-based and associative arrays are versatile and can be used for various data storage and retrieval needs in PHP. Depending on your requirements, you can choose the appropriate type of array to use in your code.

Indexed Array

In PHP, an indexed array is a type of array where elements are accessed by their numeric index. Indexed arrays are also commonly referred to as numerically indexed arrays or simply numeric arrays. Here’s how you can create and work with indexed arrays in PHP:

Creating an Indexed Array:

You can create an indexed array using the array() constructor or square brackets []. Here are examples of both methods:

// Using array() constructor

$fruits = array(“apple”, “banana”, “cherry”);

// Using square brackets (PHP 5.4+)

$fruits = [“apple”, “banana”, “cherry”];

In the above examples, we’ve created an indexed array named $fruits that contains three elements with numeric indexes: 0, 1, and 2.

Accessing Elements in an Indexed Array:

To access elements in an indexed array, use the numeric index within square brackets:

$fruits = [“apple”, “banana”, “cherry”];

echo $fruits[0]; // Outputs: apple echo $fruits[1];

// Outputs: banana

echo $fruits[2];

// Outputs: cherry

Modifying Elements in an Indexed Array:

You can modify elements in an indexed array by assigning a new value to a specific index:

$fruits = [“apple”, “banana”, “cherry”];

$fruits[1] = “kiwi”; // Update the second element

echo $fruits[1]; // Outputs: kiwi

Adding Elements to an Indexed Array:

To add elements to the end of an indexed array, you can use the [] syntax with an index that doesn’t already exist:

$fruits = [“apple”, “banana”, “cherry”];

 $fruits[3] = “orange”; // Add a new element to the end

 echo $fruits[3]; // Outputs: orange

Removing Elements from an Indexed Array:

You can remove elements from an indexed array using the unset() function or by setting an element’s value to null:

$fruits = [“apple”, “banana”, “cherry”];

unset($fruits[1]); // Remove the second element

echo $fruits[1]; // Outputs: (nothing, the element is unset)

Indexed arrays are useful when you need to work with ordered collections of data, such as lists or arrays of items that don’t require custom keys. They provide efficient and straightforward access to elements using numeric indexes.

Associative Array

In PHP, an associative array is a type of array where elements are accessed by custom keys (usually strings) that you assign to them, instead of numeric indexes. Each element in an associative array is associated with a specific key-value pair. Here’s how you can create and work with associative arrays in PHP:

Creating an Associative Array:

You can create an associative array using square brackets [] or the array() constructor by specifying key-value pairs. Here are examples of both methods:

phpCopy code

// Using square brackets (PHP 5.4+) $person = [ “first_name” => “John”, “last_name” => “Doe”, “age” => 30 ]; // Using array() constructor $person = array( “first_name” => “John”, “last_name” => “Doe”, “age” => 30 );

In the above examples, we’ve created an associative array named $person with custom keys (“first_name,” “last_name,” and “age”) associated with their respective values.

Accessing Elements in an Associative Array:

To access elements in an associative array, use the custom key within square brackets:

phpCopy code

$person = [ “first_name” => “John”, “last_name” => “Doe”, “age” => 30 ]; echo $person[“first_name”]; // Outputs: John echo $person[“last_name”]; // Outputs: Doe echo $person[“age”]; // Outputs: 30

Modifying Elements in an Associative Array:

You can modify elements in an associative array by assigning a new value to a specific key:

phpCopy code

$person = [ “first_name” => “John”, “last_name” => “Doe”, “age” => 30 ]; $person[“age”] = 31; // Update the “age” element echo $person[“age”]; // Outputs: 31

Adding Elements to an Associative Array:

To add elements to an associative array, simply assign a new key-value pair:

phpCopy code

$person = [ “first_name” => “John”, “last_name” => “Doe” ]; $person[“age”] = 30; // Add a new key-value pair echo $person[“age”]; // Outputs: 30

Removing Elements from an Associative Array:

You can remove elements from an associative array using the unset() function:

phpCopy code

$person = [ “first_name” => “John”, “last_name” => “Doe”, “age” => 30 ]; unset($person[“age”]); // Remove the “age” element echo $person[“age”]; // Outputs: (nothing, the element is unset)

Associative arrays are useful when you need to store and access data using meaningful and descriptive keys. They provide a convenient way to represent structured data and are commonly used in PHP for tasks like storing configuration settings, database query results, and more.