In this article, We will demonstrate the use of
an arrays in PHP Programming Language. This article is mainly for beginner of PHP programmers to give them
a push in the right professional direction.
We will discus the all kind of Arrays in PHP and will explain the PHP Arrays with
the short, real and professional Array Examples. Further here will provide a
complete list of PHP Array Functions; from these functions we will use not all
of them but we are sure after reading this article you will be able to use the
all PHP functions given at the end of this article.
an Array in PHP?
Arrays can most easily be described as an
ordered list. An array is a list of scalar data (If you don't know what a scalar
variable is, you should take a look at my first tutorial. Scalar data in arrays
are called 'elements'.) that is 'indexed' (so to speak) with numeric or named
keys. A 'key' is either a name or a number given to an element in an array as a
reference (if a key is numeric, it is called a 'subscript').
Example of an Basic Array Syntax in PHP
$six = 6;
$arrayname = array("this is an element",5,$six);
echo $arrayname; //it will prints: this is an element
echo $arrayname; //it will prints: 5
echo $arrayname; //it will prints: 6
As you can see, elements in an array can be any
data type of scalar data (string, integer, double) and can also be other variables.
In this example, the 'keys' are numeric (assigned automatically for you), so
when echo'ing the elements, you use the array name followed by the subscript
($arrayname[subscript]). Notice that the subscript of the first element is zero?
This will always be the case with numeric keys.
Examples of Associative Arrays in PHP
Associative arrays are arrays that use named
keys that you can assign to them in PHP. Lets look at a few examples...
$arrayone = array("key1" => "this is the first element.", "key2" => "this is the
$arraytwo = array(
"key3" => "this is the first element of the second array",
"key4" => "this is the second element of the second array",
echo $arrayone['key1']; //prints "this is the first element."
echo $arraytwo['key3']; //prints "this is the first element of the second array"
echo $arrayone['key2']; //prints "this is the second element"
echo $arraytwo['key4']; //prints "this is the second element of the second array"
Now we're getting to where you know how to
define an array (and an associative array), but you probably don't see how there
useful yet. Well think of this, say you have a bakery that sells pies. You have
3 different flavors, and each flavor has a different price. lets make this
example in php...
$bakery = array(
"cherry" => "5.00",
"apple" => "4.00",
"other" => "2.00",
echo "cherry pie costs $bakery['cherry'], apple pie costs $bakery['apple'], and
that other pie costs $bakery['other'].";
Ok, you've got an example of how to use arrays
(a very basic example, grant me that). But what if you want to give more
information on each pie? You now have the cost, but what if you wanted to add
the number of pieces you get for that price, and if the pie was sugarless or
not? One way is using multidimensional arrays in PHP.
Think of multidimensional arrays as simply a
list of arrays. This might seem confusing at first, but im sure with a few
examples (and a few pies) you will get the big picture. Pick up your fork, its
time to eat:
Examples of Multidimensional Arrays in PHP
$bakery = array(
"cherry" => array("5.00","2 pieces","sugar"),
"apple" => array("4.00","3 pieces","sugar"),
"other" => array("2.00","1 piece","sugar-free"),
echo "cherry pie costs ".$bakery['cherry'].", and you get".
//prints "cherry pie costs 5.00, and you get 2 pieces."
echo "apple pie costs ".$bakery['apple'].", and you get ".
//prints "apple pie costs 4.00, and you get 3 pieces."
echo "the other pie costs ".$bakery['other'].", but its ".
//prints "the other pie costs 2.00, but its sugar-free.
Hopefully now you will be prepared to use
arrays when you begin querying
and performing other forms of intermediate to advanced data manipulation. If
you've noticed, this 'tutorial' wasn't really a tutorial. I'm only trying to
give you the basic idea of how to define and use these arrays, not give you all
the information a person could possibly fathom. Hey, that might explain the
whole "Introduction" word in the title.
PHP Array Functions List
This will Returns an array with all string keys to lowercased or uppercased
array_chunk -- This Function will Split an array in PHP into chunks
array_count_values -- Counts all the values of an array using
array_diff_assoc -- Computes the difference of arrays with
additional index check in PHP
array_diff -- Computes the difference of arrays using PHP
array_fill -- Fill an array with values in PHP
array_filter -- Filters elements of an array using a callback
function in PHP
array_flip -- Flip all the values of an array using PHP
array_intersect_assoc -- Computes the intersection of arrays
with additional index check in PHP
array_intersect -- Computes the intersection of arrays using
array_key_exists -- Checks if the given key or index in PHP exists in
array_keys -- Return all the keys of an array using PHP
array_map -- Applies the callback to the elements of the given
arrays in PHP
array_merge_recursive -- Merge two or more arrays recursively
array_merge -- Merge two or more arrays using PHP
array_multisort -- Sort multiple or multi-dimensional arrays in
array_pad -- Pad array to the specified length with a value in
array_pop -- Pop the element off the end of array using PHP
array_push -- Push one or more elements onto the end of array
array_rand -- Pick one or more random entries out of an array
array_reduce -- Iteratively reduce the array to a single value
using a callback function in PHP
array_reverse -- Return an array with elements in reverse order
array_search -- Searches the array for a given value and
returns the corresponding key if successful in PHP
array_shift -- Shift an element off the beginning of array
array_slice -- Extract a slice of the array using PHP
array_splice -- Remove a portion of the array and replace it
with something else using PHP
array_sum -- Calculate the sum of values in an array in PHP
array_unique -- Removes duplicate values from an array using
array_unshift -- Prepend one or more elements to the beginning
of array in PHP
array_values -- Return all the values of an array in PHP
array_walk -- Apply a user function to every member of an array
array -- Create an array using PHP
arsort -- Sort an array in reverse order and maintain index
association in PHP
asort -- Sort an array and maintain index association using PHP
compact -- Create array containing variables and their values
count -- Count elements in a variable using PHP
current -- Return the current element in an array in PHP
each -- Return the current key and value pair from an array and
advance the array cursor in PHP
end -- Set the internal pointer of an array to its last element
extract -- Import variables into the current symbol table from
an array in PHP
in_array -- Return TRUE if a value exists in an array in PHP
key -- Fetch a key from an associative array in PHP
krsort -- Sort an array by key in reverse order in PHP
ksort -- Sort an array by key in PHP
list -- Assign variables as if they were an array in PHP
natcasesort -- Sort an array using a case insensitive "natural
order" algorithm in PHP
natsort -- Sort an array using a "natural order" algorithm
next -- Advance the internal array pointer of an array in
pos -- Get the current element from an array in PHP
prev -- Rewind the internal array pointer in PHP
range -- Create an array containing a range of elements
reset -- Set the internal pointer of an array to its first
element in PHP
rsort -- Sort an array in reverse order in PHP
shuffle -- Shuffle an array using PHP
sizeof -- Get the number of elements in variable of PHP Array
sort -- In PHP Sort an array
uasort -- Sort an array with a user-defined comparison function
and maintain index association in PHP
uksort -- Sort an array by keys using a user-defined comparison
function in PHP
usort -- Sort an array by values using a user-defined
comparison function using PHP