Indexed arrays and associative arrays

Indexed arrays and associative arrays

Arrays are an important user-defined data structure type in PHP programs. They can store multiple data variables as array elements. There are two main types of arrays in PHP programs: indexed arrays and associative array formats.

Indexed arrays and associative arrays

Indexed Arrays in PHP.

Indexed arrays in PHP are used to store array value elements in a continuous sequential order. By default, numeric indexes are used to store and process array element values, starting from 0 to the end of the array. Indexed arrays can be created either in explicit order by defining an index, or in indirect order by automatically assigning PHP array element indices.

Example of an indexed array in PHP.

In PHP programming, implicitly indexed arrays automatically indicate the storage location of an element, starting from 0.

<?php

$country = [“India”, “Usa”, “China”, “Uk”];

echo $country[0]; // Result – India

echo “\n”;

echo $country[1]; // Result – Usa

echo “\n”;

echo $country[2]; // Result – China

echo “\n”;

echo $country[3]; // Result – Uk

?>

Explicitly indexed arrays in PHP.

In PHP programs, programmers can define the indexes of an explicitly indexed array in an explicit order.

<?php

$country = [0 => “India”, 1 => “Usa”, 2 => “Uk”, 3 => “China”];

echo $country[0]; // Result – India

echo “\n”;

echo $country[1]; // Result – USA

echo “\n”;

echo $country[2]; // Result – UK

echo “\n”;

echo $country[3]; // Result – China

?>

In this program condition, programmers can specify the index element 0, 1, 2, 3, etc. for each array value. However, if programmers don’t specify the index, PHP will automatically indicate the numerical index location in the program, starting from 0.

Adding to an indexed array in PHP.

In a PHP program, programmers can add array elements to an indexed array by using the array_push() function method, or simply specifying the array value if needed.

<?php

$country = [“India”, “Usa” , “Uk”];

array_push($country, “Canada”, “Mexico”); // here we Adds “canada” and “Mexico” to the country array

print_r($country); // Result – Array ([0] => India [1] => Usa [2] => Uk [3] => Canada [4] => Mexico)

$country[] = “Russia”; // here we Adds “Russia” at the end of the country array

print_r($country); // Result – Array ([0] => India [1] => Usa [2] => Uk [3] => Canada [4] => Mexico [5] => Russia )

?>

Associative Array in PHP.

PHP uses the named keys string method instead of numeric indexes to store and process associative array values ​​in a program. These associative array keys are custom user-defined within the program, providing the programmer with greater flexibility in naming associative array elements.

Example of an associative array in PHP.

<?php

$employee = [

“emp_name” => “Bhavishi Deora”,

“emp_age” => 19,

“country” => “india”,

“cont” => 9414

];

echo $employee[“emp_name”]; // Result – Bhavishi Deora

echo “\n”;

echo $employee[“emp_age”]; // Result – 19

echo “\n”;

echo $employee[“country”]; // Result – India

echo “\n”;

echo $employee[“cont”]; // Result – 9414

echo “\n”;

?>

In the associative array example.

  • In this program, the named keys contain fields like “emp_name”, “emp_age”, “country”, and “cont”.
  • Here, programmers can store and process array values ​​by providing references to these array-compatible key values.

Adding to an associative array in PHP.

In the PHP associative array program, programmers can add new array elements to an associative array by specifying the array keys and values.

<?php

$employee = [

“emp_name” => “Bhavishi deora”,

“emp_age” => 19

];

$employee[“city”] = “india”; // Here we add a new city as India as a key-value pair

$employee[“id”] = “101”; // Here we add a new id as 101 as a key-value pair

$employee[“profession”] = “Developer”; // Here we add a new profession as Developer as a key-value pair

print_r($employee);

// Array ( [emp_name] => Bhavishi Deora [emp_age] => 19 [city] => India [id] => 101 [profession] => Developer)

?>

PHP programmers can also use the array_push() function method with associative arrays, although the syntax is different here. It follows a numeric index for associative array elements. Generally, programmers don’t need to use the array_push() function method for associative arrays, as they can specify the required key-value pairs directly within the array program.

Multidimensional array in PHP.

Multidimensional arrays can be declared and used as needed in PHP programs. This means that programmers can store and process both indexed and associative arrays within an array. Multidimensional arrays in PHP are commonly used to create and display more complex data structures, such as user-defined tables or mathematical matrices.

Example of a multidimensional indexed array in PHP.

<?php

$table = [

[80, 90, 100],

[34, 94, 11],

[33, 44, 66]

];

echo $table[0][2]; // Result – 100

echo “\n”;

echo $table[1][1]; // Result – 94

echo “\n”;

echo $table[2][2]; // Result – 66

?>

Example of a multidimensional associative array in PHP.

<?php

$employee = [

“Harry” => [“landline” => “111-222-333”, “profession” => “designer”],

“Siddhi” => [“landline” => “233-777-888”, “email” => “siddhi@domain.com”]

];

echo $employee[“Harry”][“profession”]; //Result-designer

echo “\n”;

echo $employee[“Siddhi”][“landline”]; // Result – 233-777-888

?>

Main Differences Between Indexed and Associative Arrays in php.

Array FeatureIndexed Array explanationAssociative Array explanation
Index array data typeIndexed Array as store array element In Numeric format like (0, 1, 2, 3, …)Associative Array used as String or custom key (e.g. “emp_name”, “emp_age”) to index data type
How to Accessing array ElementsIndexed Array element access By numerical index (e.g. $array[0])Associative Array By access its element as key (e.g. $array[“emp_name”])
Multiple Use Case scenarioIn Indexed Array When order is important and data can be accessed through its numerical index locationIn Associative Array When elements need to be accessed by custom labels or user define custom keys names
Array syntax$array = [“india”, “usa”, “china”];$array = [“emp_name” => “siddhi”, “emp_age” => 21];

Summary of indexed and associative arrays in PHP.

  • Indexed arrays – Data in a PHP array program is stored and processed using a numeric index, starting at 0. This is typically used when ordering array elements is preferred.
  • Associative tables – Use the custom keys/strings method to store data elements in an associative array program. Here, you can add labels to an associative array value, such as “emp_name” => “Harry”.
  • Multidimensional tables – Arrays within arrays in a PHP array program are used to create more complex array data element structures.

Leave a Reply