Wednesday, February 25, 2026

PHP Arrays

 

PHP Arrays

In PHP, arrays are versatile data structures that allow you to store multiple values in a single variable. They can hold values of different types (strings, integers, objects, even other arrays) and are widely used in web development for handling collections of data.


🌍 Types of Arrays in PHP

  • Indexed Arrays: Use numeric indexes.
$fruits = array("Apple", "Banana", "Cherry");
echo $fruits[1]; // Banana
  • Associative Arrays: Use named keys.
$student = array("name" => "Alice", "age" => 22);
echo $student["name"]; // Alice
  • Multidimensional Arrays: Arrays containing other arrays.
$matrix = array(
  array(1, 2, 3),
  array(4, 5, 6)
);
echo $matrix[1][2]; // 6

🛠️ Common PHP Array Functions

Here’s a list of frequently used functions that work on arrays:

  • array(): Creates an array.

  • array_change_key_case(): Changes all keys to uppercase or lowercase.

  • array_chunk(): Splits an array into smaller arrays.

  • array_column(): Returns values from a single column.

  • array_combine(): Creates an array using one array for keys and another for values.

  • array_count_values(): Counts occurrences of values.

  • array_diff(): Returns differences between arrays (values only).

  • array_diff_assoc(): Returns differences with key-value checks.

  • array_diff_key(): Returns differences based on keys.

  • array_merge(): Merges multiple arrays.

  • array_pop(): Removes and returns the last element.

  • array_push(): Adds elements to the end.

  • array_shift(): Removes and returns the first element.

  • array_unshift(): Adds elements to the beginning.

  • array_slice(): Extracts a portion of an array.

  • array_splice(): Removes/replaces elements.

  • array_unique(): Removes duplicate values.

  • in_array(): Checks if a value exists in an array.

  • array_key_exists(): Checks if a key exists.

  • array_keys(): Returns all keys.

  • array_values(): Returns all values.

    W3School PHP GeeksForGeeks


📖 Conclusion

PHP arrays are flexible and powerful, supporting indexed, associative, and multidimensional structures. With a wide range of built-in functions, developers can manipulate arrays efficiently—whether splitting, merging, filtering, or searching.

No comments:

Post a Comment

Support Vector Machines in Machine Learning

Support Vector Machines in Machine Learning Introduction Support Vector Machines (SVMs) are powerful supervised learning algorithms used ...