Google News
logo
PHP Programs to array
In PHP, an array is a collection of values that can be of any data type, such as strings, integers, floats, or even other arrays. Arrays can be indexed by a numeric index or a string index, and can be of fixed size or dynamic size.

To create an array in PHP, you can use the array() function, which takes any number of comma-separated values and returns an array containing those values. For example, the following code creates an array with three elements :
$myArray = array("apple", "banana", "orange");​
Arrays can also be created using short array syntax, which was introduced in PHP 5.4. This syntax uses square brackets to define the array, and is generally considered more concise and easier to read. For example, the following code creates the same array using short array syntax:
$myArray = ["apple", "banana", "orange"];​

Arrays can be accessed using their index, which can be either a numeric index or a string index. Numeric indices start at 0, while string indices can be any string. For example, to access the first element of the array above, you would use the following code :
echo $myArray[0];  // Outputs "apple"​
Arrays can also be looped through using a for loop or a foreach loop. For example, the following code loops through the array and outputs each element :
foreach ($myArray as $value) {
    echo $value . "\n";
}​
PHP provides a number of built-in functions for working with arrays, such as array_push(), array_pop(), array_shift(), and array_unshift(), which can be used to add or remove elements from the array.