Loading...
Loading...
00:00:00

PHP Array 

In PHP, an array is a data structure that can store multiple values in a single variable. Each value in an array is assigned a unique index that can be used to access the value later. Arrays can be used to store a wide variety of data types, including strings, numbers, and objects.

PHP supports three types of arrays:

  1. Indexed arrays: An indexed array is an array where each element is assigned a numerical index, starting from 0. Indexed arrays can be created using the array() function or using square brackets [].
// Using array()
$fruits = array("apple", "banana", "orange");

// Using []
$fruits = ["apple", "banana", "orange"];
  1. Associative arrays: An associative array is an array where each element is assigned a string key that can be used to access the value later. Associative arrays can be created using the array() function or using square brackets [].
// Using array()
$ages = array("Peter" => 23, "John" => 32, "Mary" => 28);

// Using []
$ages = ["Peter" => 23, "John" => 32, "Mary" => 28];
  1. Multi-dimensional arrays: A multi-dimensional array is an array that contains one or more arrays as its elements. Multi-dimensional arrays can be created using nested array() functions or nested square brackets [].
// Using array()
$employees = array(
    array("name" => "John", "age" => 32, "department" => "Sales"),
    array("name" => "Mary", "age" => 28, "department" => "Marketing"),
    array("name" => "Peter", "age" => 23, "department" => "IT")
);

// Using []
$employees = [    ["name" => "John", "age" => 32, "department" => "Sales"],
    ["name" => "Mary", "age" => 28, "department" => "Marketing"],
    ["name" => "Peter", "age" => 23, "department" => "IT"]
];

PHP Array functions

In PHP, an array is a data structure that stores multiple values in a single variable. Each value in an array is assigned a unique index that can be used to access the value later. Arrays can be used to store a wide variety of data types, including strings, numbers, and objects.

Here are some of the most commonly used array functions in PHP:

  1. array() - Creates a new array.
  2. count() - Returns the number of elements in an array.
  3. is_array() - Checks whether a variable is an array.
  4. sort() - Sorts an array in ascending order.
  5. rsort() - Sorts an array in descending order.
  6. asort() - Sorts an array in ascending order, preserving keys.
  7. ksort() - Sorts an array by key in ascending order.
  8. usort() - Sorts an array using a user-defined comparison function.
  9. array_push() - Adds one or more elements to the end of an array.
  10. array_pop() - Removes the last element from an array.
  11. array_shift() - Removes the first element from an array.
  12. array_unshift() - Adds one or more elements to the beginning of an array.
  13. array_merge() - Combines two or more arrays into a single array.
  14. array_slice() - Extracts a portion of an array.
  15. array_search() - Searches an array for a given value and returns the corresponding key if found.

Here are examples of how to use some of these array functions:

// array()
$colors = array("red", "green", "blue");

// count()
$colors = array("red", "green", "blue");
$numberOfColors = count($colors); // $numberOfColors is 3

// is_array()
$colors = array("red", "green", "blue");
$isAnArray = is_array($colors); // $isAnArray is true

// sort()
$numbers = array(3, 1, 4, 2);
sort($numbers); // $numbers is now [1, 2, 3, 4]

// rsort()
$numbers = array(3, 1, 4, 2);
rsort($numbers); // $numbers is now [4, 3, 2, 1]

// asort()
$ages = array("Peter" => 23, "John" => 32, "Mary" => 28);
asort($ages); // $ages is now ["Peter" => 23, "Mary" => 28, "John" => 32]

// ksort()
$ages = array("Peter" => 23, "John" => 32, "Mary" => 28);
ksort($ages); // $ages is now ["John" => 32, "Mary" => 28, "Peter" => 23]

// usort()
$fruits = array("apple", "orange", "banana", "pear");
usort($fruits, function($a, $b) {
    return strlen($a) - strlen($b);
});
// $fruits is now ["pear", "apple", "orange", "banana"]

// array_push()
$colors = array("red", "green", "blue");
array_push($colors, "yellow"); // $colors is now ["red", "green", "blue", "yellow"]

// array_pop()
$colors = array("red", "green", "blue");
$lastColor = array_pop($colors); // $lastColor is "blue", $colors is now ["red", "green"]