PHP Arrays

In PHP, an array is a data structure that allows you to store a collection of values of the same data type under a single variable name. You can access the values in an array using an index, which is a numerical value that identifies the position of a value within the array.

Here are some examples of using arrays in PHP:

  1. Numeric arrays – Used to store a collection of values indexed by their position
$colors = array("red", "green", "blue");
echo $colors[0]; // Output: red
echo $colors[1]; // Output: green
echo $colors[2]; // Output: blue

In this example, an array named $colors is defined that contains the values "red", "green", and "blue". The values are accessed using their index position within the array, starting with index 0.

  1. Associative arrays – Used to store a collection of values indexed by a custom key
$person = array("name" => "John", "age" => 30, "gender" => "male");
echo $person["name"]; // Output: John
echo $person["age"]; // Output: 30
echo $person["gender"]; // Output: male

In this example, an array named $person is defined that contains the values "John", 30, and "male", indexed by the keys "name", "age", and "gender", respectively. The values are accessed using their custom keys within the array.

  1. Multidimensional arrays – Used to store arrays within arrays
$matrix = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);
echo $matrix[0][0]; // Output: 1
echo $matrix[1][2]; // Output: 6
echo $matrix[2][1]; // Output: 8

In this example, a multidimensional array named $matrix is defined that contains three arrays, each with three values. The values are accessed using their index positions within the nested arrays.

You can also use built-in functions to manipulate arrays, such as count() to get the number of elements in an array, array_push() to add an element to the end of an array, and array_pop() to remove the last element from an array.





$numbers = array(1, 2, 3, 4, 5);
$count = count($numbers);
echo "The array has $count elements"; // Output: The array has 5 elements
array_push($numbers, 6);
print_r($numbers); // Output: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 )
$last = array_pop($numbers);
echo "The last element is $last"; // Output: The last element is 6

By using arrays in PHP, you can store and manipulate large amounts of data efficiently and effectively, and perform complex operations on your data with ease.

  1. Sorting an array using sort() function




$numbers = array(3, 1, 4, 2, 5);
sort($numbers);
print_r($numbers);

In this example, the sort() function is used to sort the values in the $numbers array in ascending order. The print_r() function is used to print the sorted array to the screen. The output will be: Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ).

By using different functions and methods available in PHP, you can manipulate and transform indexed arrays to suit your needs, and make complex operations on data structures such as matrices and tables.

  1. Creating an indexed array from a string using explode() function




$string = "apple,banana,orange";
$fruits = explode(",", $string);
print_r($fruits);

In this example, the explode() function is used to create an indexed array named $fruits from a comma-separated string of values. The resulting array contains three elements: apple, banana, and orange. The print_r() function is used to print the array to the screen. The output will be: Array ( [0] => apple [1] => banana [2] => orange ).