PHP Arrays

In PHP, an array is a data structure that allows you to store and access a collection of values. There are two types of arrays in PHP: indexed arrays and associative arrays.

An indexed array is an array that is accessed using a numeric index, with the first element having an index of 0 and the second element having an index of 1, and so on. Here is an example of creating an indexed array in PHP:

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

You can also create an indexed array using the square bracket notation:

$numbers[0] = 1;
$numbers[1] = 2;
$numbers[2] = 3;
$numbers[3] = 4;
$numbers[4] = 5;

You can access the elements of an indexed array using the square bracket notation:

echo $numbers[0]; // output: 1

An associative array is an array that is accessed using a string key, where each element has a key-value pair. Here is an example of creating an associative array in PHP:

$person = array("name" => "John", "age" => 25, "gender" => "male");

You can also create an associative array using the square bracket notation:

$person["name"] = "John";
$person["age"] = 25;
$person["gender"] = "male";

You can access the elements of an associative array using the square bracket notation:

echo $person["name"]; // output: John

PHP also provides several built-in functions for working with arrays, such as count() to get the number of elements in an array, sort() to sort the elements of an array, and implode() to join the elements of an array into a string.

arrays in PHP start

with an index of 0, not 1. Also, the elements in an array can be of any data type, such as integers, strings, floats, or even other arrays. And, arrays are dynamic in nature, so you can add, remove or modify the elements in an array at any time.

Additionally, in PHP you also have the option to use the ArrayObject class which provides an object-oriented interface for arrays and a lot of useful methods for working with arrays like ArrayAccess, Iterator, Countable and Serializable.

It’s also worth noting that multidimensional arrays can also be used in PHP. A multidimensional array is an array containing one or more arrays. For example, a 2D array can be represented as an array of arrays, where each element of the main array is an array itself.

$cars = array(
  array("Volvo",22,18),
  array("BMW",15,13),
  array("Saab",5,2),
  array("Land Rover",17,15)
);

And you can access the elements in this 2D array like this:

echo $cars[0][0]; // output: Volvo

Here are a few more things to keep in mind when working with arrays in PHP:

  • The array functions in PHP are case-sensitive, so make sure to use the correct case when calling them.
  • You can use the var_dump() function to check the contents of an array and get information about its data type and size.
  • You can use the print_r() function to display the contents of an array in a human-readable format.
  • You can use the array_merge() function to merge two or more arrays into a single array.
  • You can use the array_diff() function to compare two arrays and get the values that are present in one array but not in the other.
  • You can use the array_keys() function to get all the keys of an array, and the array_values() function to get all the values of an array.
  • You can use the in_array() function to check if a certain value exists in an array.
  • You can use the array_search() function to search for a value in an array and get its key.
  • You can use the array_slice() function to get a portion of an array.

There are many more array functions and methods available in PHP. I recommend checking out the PHP documentation for a full list of array functions and their usage.

During my Internship I used PHP a lot, had to build a whole bunch of stuff with PHP I would use alot the PHP manual from php.net and w3 schools.

Here are some sources:

https://www.w3schools.com/php/php_arrays.asp

https://www.php.net/manual/en/index.php

This is all for now!

PEACE!