Skip to main content

Define an Array in PHP

An array is an ordered list of data. PHP arrays are associative, meaning that you can access the data by its key rather than its position in the array.

PHP arrays can be used for a variety of purposes. They can be treated as simple arrays, lists, hash tables, dictionaries, collections, stacks, or queues. Additionally, they can be used to store other arrays, trees, or multidimensional arrays. This makes PHP arrays extremely versatile and flexible. While they may not be the best choice for every situation, they can certainly be adapted to meet a wide range of needs. As a result, PHP arrays are often the go-to choice for many developers.

Arrays can be created using the array function or the square brackets:

$fruit = array('apple','banana','orange','mango');
$fruit = ['apple','banana','orange','mango'];
$settings = [
    'send_from_phone' => '',
    'days_before_notify' => '0',
    'time_to_notify' => '09:00',
    'send_from_email' => ''
];
$settings = [
    'receipents' => ['[email protected]', '[email protected]', '[email protected]'],
    'working_days' => ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'],
];

By continuing to use the site, you agree to the use of cookies.