1. Home
  2. Docs
  3. Web Technology II
  4. Strings and Arrays
  5. Array in PHP

Array in PHP

An array in PHP is a special variable that can hold multiple values. Instead of declaring individual variables for each value, you can use an array to store a list of values.

  • Arrays can store different types of data, such as strings, integers, and even other arrays.
  • Numerically Indexed Arrays
  • Associative Arrays
  • Multidimensional Arrays

1.) Numerically Indexed Arrays

A numerically indexed array is an array where each element is associated with a numerical index, starting from 0. These are used to store ordered collections of values.

  • It is useful for lists or sequential data where order is important.
$fruits = array("Apple", "Banana", "Cherry");
echo $fruits[0]; // Output: Apple

2.) Associative Arrays

An associative array uses named keys instead of numeric indexes to access its elements. These keys act as identifiers for the values, making it easier to work with data that has labels.

  • It is useful for representing data with meaningful labels.
$person = array(
    "name" => "John",
    "age" => 30,
    "job" => "Developer"
);
echo $person["name"]; // Output: John

3.) Multidimensional Arrays

A multidimensional array is an array that contains other arrays as its elements. This type of array is used to represent more complex data structures like tables or matrices.

  • It is useful for storing structured data like rows and columns.
$students = array(
    array("John", 25, "A"),
    array("Alice", 23, "B"),
    array("Bob", 24, "A")
);
echo $students[1][0]; // Output: Alice
  • count()
  • array_push()
  • array_pop()
  • array_merge()
  • in_array()
  • array_search()

1.) count()

  • Returns the number of elements in an array.
$fruits = array("Apple", "Banana", "Orange");
echo count($fruits);  // Output: 3

2.) array_push()

  • Adds one or more elements to the end of an array.
$fruits = array("Apple", "Banana");
array_push($fruits, "Orange");
print_r($fruits);  // Output: Array ( [0] => Apple [1] => Banana [2] => Orange )

3.) array_pop()

  • Removes the last element of an array.
$fruits = array("Apple", "Banana", "Orange");
array_pop($fruits);
print_r($fruits);  // Output: Array ( [0] => Apple [1] => Banana )

4.) array_merge()

  • Merges two or more arrays into one.
$array1 = array("Apple", "Banana");
$array2 = array("Orange", "Grapes");
$merged = array_merge($array1, $array2);
print_r($merged);  // Output: Array ( [0] => Apple [1] => Banana [2] => Orange [3] => Grapes )

5.) in_array()

  • Checks if a value exists in an array.
$fruits = array("Apple", "Banana", "Orange");
if (in_array("Banana", $fruits)) {
    echo "Banana is in the array!";
}
// Output: Banana is in the array!

6.) array_search()

  • Returns the key of the value found in the array.
$fruits = array("Apple", "Banana", "Orange");
$key = array_search("Banana", $fruits);
echo $key;  // Output: 1

Regular expressions (regex) are patterns used to match character combinations in strings. PHP provides functions to use regular expressions for pattern matching and manipulation.

  • preg_match()
  • preg_replace()
  • preg_match_all()
  • preg_split()

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *