Saturday, April 2, 2016

Understanding PHP Arrays

Arrays in PHP are a very useful tool to use when a developer wants to put several valuables in a list or table format. This makes it easy to categorize and retrieve values from one variable. There are three types of arrays in PHP: indexed, associative, and multidimensional.
  • An indexed array is where the indexes of the array are integers, starting with 0.
    An example of assignment:
    $example_array[0] = 1;
  • An associative array is where the indexes of the array are alphanumeric, which means that the developer can name the index in either letters, numbers, or both.
    An example of assignment:
    $example_array['George'] = 45;
  • A multidimensional array is where an array lies within another array. The developer can nest arrays so that there can be multiple dimensions, like a 3 x 4 table. 
The array variable also comes with a multitude of preset functions such as count() and array(). The array() function is particularly useful since it saves the time of writing out the name of the array repetitively when assigning multiple values. With this function, the developer can insert large amounts of values at once in one command line:
  • $example_array = array("Beth", "Andrew", "Grace");
Through using a for or foreach loop, it is easy to retrieve values from one variable instead of trying to retrieve a value from a large amount of separate variables. There can be many ways to write out these loops since there are many purposes that it may serve such as sorting, retrieving, print out, and much more.

Arrays are excellent tools to use when handling large amounts of data, and they come with a variety of functions as well. Here is a helpful video:


Source: http://www.w3schools.com/php/php_arrays.asphttp://www.w3schools.com/php/php_arrays_multi.asp

4 comments: