techmore.in

PHP - Arrays

Arrays in PHP are versatile and allow you to store multiple values in a single variable. They are essential for handling collections of data, such as lists of items, sets of key-value pairs, and more complex data structures. Here's a comprehensive overview of how arrays work in PHP:

1. Indexed Arrays

Indexed arrays are arrays where each element is assigned a numeric index starting from 0.

Creating Indexed Arrays

You can create indexed arrays using the array() construct or using square brackets [] since PHP 5.4.

Example:

php
<?php // Using array() construct $colors = array("Red", "Green", "Blue"); // Using square brackets [] $numbers = [1, 2, 3, 4, 5]; ?>

Accessing Elements

You can access elements of an indexed array using their numeric index.

Example:

php
<?php echo $colors[0]; // Outputs: Red echo $numbers[2]; // Outputs: 3 ?>

Modifying Elements

You can modify elements of an indexed array by assigning a new value to a specific index.

Example:

php
<?php $colors[1] = "Yellow"; print_r($colors); // Outputs: Array ( [0] => Red [1] => Yellow [2] => Blue ) ?>

2. Associative Arrays

Associative arrays are arrays where each element is associated with a specific key instead of a numeric index.

Creating Associative Arrays

You can create associative arrays using the array() construct or using square brackets [].

Example:

php
<?php // Using array() construct $person = array( "name" => "John", "age" => 30, "city" => "New York" ); // Using square brackets [] $book = [ "title" => "PHP Handbook", "author" => "John Doe", "year" => 2023 ]; ?>

Accessing Elements

You can access elements of an associative array using their keys.

Example:

php
<?php echo $person["name"]; // Outputs: John echo $book["author"]; // Outputs: John Doe ?>

Modifying Elements

You can modify elements of an associative array by assigning a new value to a specific key.

Example:

php
<?php $person["age"] = 35; print_r($person); // Outputs: Array ( [name] => John [age] => 35 [city] => New York ) ?>

3. Multidimensional Arrays

Multidimensional arrays are arrays that contain other arrays as elements. They can be indexed or associative.

Creating Multidimensional Arrays

Example of Indexed Multidimensional Array:

php
<?php $matrix = array( array(1, 2, 3), array(4, 5, 6), array(7, 8, 9) ); ?>

Example of Associative Multidimensional Array:

php
<?php $users = array( "user1" => array( "name" => "John", "age" => 30 ), "user2" => array( "name" => "Jane", "age" => 25 ) ); ?>

Accessing Elements in Multidimensional Arrays

You can access elements in multidimensional arrays using multiple indices.

Example:

php
<?php echo $matrix[1][2]; // Outputs: 6 echo $users["user1"]["name"]; // Outputs: John ?>

4. Array Functions

PHP provides a variety of built-in functions to manipulate arrays, such as:

  • count(): Returns the number of elements in an array.
  • array_push(): Adds one or more elements to the end of an array.
  • array_pop(): Removes and returns the last element of an array.
  • array_merge(): Merges one or more arrays into a single array.
  • array_key_exists(): Checks if a specified key exists in an array.

Example:

php
<?php $numbers = [1, 2, 3, 4, 5]; echo count($numbers); // Outputs: 5 array_push($numbers, 6); print_r($numbers); // Outputs: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 ) $last = array_pop($numbers); echo $last; // Outputs: 6 $newArray = array_merge($colors, $numbers); print_r($newArray); // Outputs: Array ( [0] => Red [1] => Green [2] => Blue [3] => 1 [4] => 2 [5] => 3 [6] => 4 [7] => 5 ) if (array_key_exists("name", $person)) { echo "Name exists in person array."; } ?>