PHP Arrays

PHP Arrays

In PHP, an array is a data structure that allows you to store multiple values in a single variable. There are different types of arrays in PHP, including indexed arrays, associative arrays, and multidimensional arrays. Arrays are useful when you need to work with collections of data, such as a list of items, key-value pairs, or tables.

Types of Arrays in PHP

  1. Indexed Arrays
  2. Associative Arrays
  3. Multidimensional Arrays

1. Indexed Arrays

An indexed array uses numeric indexes (starting from 0 by default) to store values. The values are stored in a specific order, and each value is assigned an index that corresponds to its position in the array.

Syntax:

$array_name = array(value1, value2, value3);

Or using the short array syntax:

$array_name = [value1, value2, value3];

Example:

$fruits = ["Apple", "Banana", "Cherry"]; echo $fruits[0]; // Outputs: Apple echo $fruits[1]; // Outputs: Banana echo $fruits[2]; // Outputs: Cherry
  • In this example, the array $fruits contains 3 items. The first item ("Apple") is at index 0, the second item ("Banana") is at index 1, and the third item ("Cherry") is at index 2.

2. Associative Arrays

An associative array uses named keys (strings) rather than numeric indexes to store values. This allows you to map a unique key to each value.

Syntax:

$array_name = array("key1" => value1, "key2" => value2, "key3" => value3);

Or using the short array syntax:

$array_name = ["key1" => value1, "key2" => value2, "key3" => value3];

Example:

$person = [ "name" => "John", "age" => 30, "city" => "New York" ]; echo $person["name"]; // Outputs: John echo $person["age"]; // Outputs: 30 echo $person["city"]; // Outputs: New York
  • In this example, the array $person uses keys like "name", "age", and "city" to store their respective values.

3. Multidimensional Arrays

A multidimensional array is an array that contains other arrays. It's used to store more complex data structures, such as tables or grids.

Syntax:

$array_name = array( array(value1, value2), array(value3, value4) );

Example:

$matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]; echo $matrix[0][0]; // Outputs: 1 echo $matrix[1][2]; // Outputs: 6 echo $matrix[2][1]; // Outputs: 8
  • In this example, $matrix is a 2D array (array of arrays). You can access each element by specifying two indexes: the first for the outer array and the second for the inner array.

Common Array Functions in PHP

PHP provides many built-in functions to work with arrays. Here are some of the most commonly used ones:

1. count()

Returns the number of elements in an array.

$fruits = ["Apple", "Banana", "Cherry"]; echo count($fruits); // Outputs: 3

2. array_push()

Adds one or more elements to the end of an array.

$fruits = ["Apple", "Banana"]; array_push($fruits, "Cherry", "Grape"); print_r($fruits); // Outputs: Array ( [0] => Apple [1] => Banana [2] => Cherry [3] => Grape )

3. array_pop()

Removes the last element from an array.

$fruits = ["Apple", "Banana", "Cherry"]; array_pop($fruits); print_r($fruits); // Outputs: Array ( [0] => Apple [1] => Banana )

4. array_shift()

Removes the first element from an array.

$fruits = ["Apple", "Banana", "Cherry"]; array_shift($fruits); print_r($fruits); // Outputs: Array ( [0] => Banana [1] => Cherry )

5. array_unshift()

Adds one or more elements to the beginning of an array.

$fruits = ["Banana", "Cherry"]; array_unshift($fruits, "Apple"); print_r($fruits); // Outputs: Array ( [0] => Apple [1] => Banana [2] => Cherry )

6. in_array()

Checks if a value exists in an array.

$fruits = ["Apple", "Banana", "Cherry"]; if (in_array("Banana", $fruits)) { echo "Banana is in the array."; } // Outputs: Banana is in the array.

7. array_keys()

Returns all the keys of an array.

$person = ["name" => "John", "age" => 30, "city" => "New York"]; $keys = array_keys($person); print_r($keys); // Outputs: Array ( [0] => name [1] => age [2] => city )

8. array_values()

Returns all the values of an array.

$person = ["name" => "John", "age" => 30, "city" => "New York"]; $values = array_values($person); print_r($values); // Outputs: Array ( [0] => John [1] => 30 [2] => New York )

9. array_merge()

Merges two or more arrays.

$array1 = ["Apple", "Banana"]; $array2 = ["Cherry", "Grape"]; $merged = array_merge($array1, $array2); print_r($merged); // Outputs: Array ( [0] => Apple [1] => Banana [2] => Cherry [3] => Grape )

10. array_slice()

Extracts a portion of an array.

$fruits = ["Apple", "Banana", "Cherry", "Grape"]; $sliced = array_slice($fruits, 1, 2); print_r($sliced); // Outputs: Array ( [0] => Banana [1] => Cherry )

Accessing Array Elements

You can access array elements using their index (for indexed arrays) or key (for associative arrays).

Example 1: Indexed Array

$colors = ["Red", "Green", "Blue"]; echo $colors[1]; // Outputs: Green

Example 2: Associative Array

$person = ["name" => "John", "age" => 30]; echo $person["name"]; // Outputs: John

Summary

  • Indexed Arrays: Use numeric indexes to access values. Good for lists or collections with no key-value pairs.
  • Associative Arrays: Use named keys to access values. Useful for mapping data to descriptive keys.
  • Multidimensional Arrays: Arrays inside arrays. Used for more complex data structures (e.g., tables or grids).
  • Common Array Functions: PHP provides numerous functions like count(), array_push(), array_pop(), array_shift(), etc., to manipulate and interact with arrays.

Arrays are an essential data structure in PHP and are used extensively to handle large collections of data efficiently.

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close