Arrays In Php

PHP Arrays :

Arrays are sets of data which can be defined in a PHP Script. Arrays can contain other arrays inside of them without any restriction (hence building multidimensional arrays). Arrays can be referred to as tables or hashes.

Syntax :

Arrays can be created in two ways. The first involves using the function array. The second involves using square brackets.

The array function method :

In the array function method, you create a array in the scheme of: $foo = bar()

For example, to set the array up to make the keys sequential numbers (Example: "0, 1, 2, 3"), you use: $foobar = array($foo, $bar);

This would produce the array like this:
$foobar[0] = $foo
$foobar[1] = $bar

It is also possible to define the key value:
$foobar = array('foo' => $foo, 'bar' => $bar);

This would set the array like this: $foobar['foo'] = $foo
$foobar['bar'] = $bar

The square brackets method

The square brackets method allows you to set up by directly setting the values. For example, to make $foobar[1] = $foo, all you need to do is: $foobar[1] = $foo;

The same applies for setting the key value: $foobar['foo'] = $foo;

Examples of Arrays

Example #1 This example sets and prints arrays.

CODE/PROGRAM/EXAMPLE
//PHP Code:
<?php
$array =
array("name"=>"Toyota","type"=>"Celica","colour"=>"black","manufactured"=>"1991");
$array2 = array("Toyota","Celica","black","1991");
$array3 = array("name"=>"Toyota","Celica","colour"=>"black","1991");
print_r($array);
print_r($array2);
print_r($array3);
?>

//  O/P : Array
(
[name] => Toyota
[type] => Celica
[colour] => black
[manufactured] => 1991
)
Array
(
[0] => Toyota
[1] => Celica
[2] => black
[3] => 1991
)
Array
(
[name] => Toyota
[0] => Celica
[colour] => black
[1] => 1991
)

HTML Render:
Array ( [name] => Toyota [type] => Celica [colour] => black [manufactured] => 1991 ) Array
( [0] => Toyota [1] => Celica [2] => black [3] => 1991 ) Array ( [name] => Toyota [0] =>
Celica [colour] => black [1] => 1991 )

Example #2 The following example will output the identical text as Example #1:

CODE/PROGRAM/EXAMPLE
<?php
$array["name"]="Toyota";
$array["type"]="Celica";
$array["colour"]="black";
$array["manufactured"]="1991";
$array2[]="Toyota";
$array2[]="Celica";
$array2[]="black";
$array2[]="1991";
$array3["name"]="Toyota";
$array3[]="Celica";
$array3[colour"]="black";
$array3[]="1991";
print_r($array);
print_r($array2);
print_r($array3);
?>

Example #3 Using the Example #1 and Example #2 above, now you can try and use arrays the same way as normal variables:

CODE/PROGRAM/EXAMPLE
//PHP Code:
<?php
echo "Manufacturer: &lt;b&gt;{$array["name"]}&lt;/b&gt;&lt;br /&gt;\n";
echo "Brand: &lt;b&gt;{$array2["1"]}&lt;/b&gt;&lt;br /&gt;\n";
echo "Colour: &lt;b&gt;".$array3["colour"]."&lt;/b&gt;&lt;br /&gt;\n";
echo "Year Manufactured: &lt;b&gt;".$array3[1]."&lt;/b&gt;&lt;br /&gt;\n"
?>

//  O/P : Manufacturer: <b>Toyota</b><br />
    Brand: <b>Celica</b><br />
    Colour: <b>black</b><br />
    Year Manufactured: <b>1991</b><br />

HTML Render:
Manufacturer: Toyota
Brand: Celica
Colour: black
Year Manufactured: 1991

A Numerically Indexed Array :

If this is your first time seeing an array, then you may not quite understand the concept of an array. Imagine that you own a business and you want to store the names of all your employees in a PHP variable. How would you go about this?

It wouldn't make much sense to have to store each name in its own variable. Instead, it would be nice to store all the employee names inside of a single variable. This can be done, and we show you how below.

CODE/PROGRAM/EXAMPLE
//PHP Code:
$employee_array[0] = "Bob";
$employee_array[1] = "Sally";
$employee_array[2] = "Charlie";
$employee_array[3] = "Clare";

In the above example we made use of the key / value structure of an array. The keys were the numbers we specified in the array and the values were the names of the employees. Each key of an array represents a value that we can manipulate and reference. The general form for setting the key of an array equal to a value is:

Syntax
$array[key] = value;

If we wanted to reference the values that we stored into our array, the following PHP code would get the job done.

CODE/PROGRAM/EXAMPLE
//PHP Code:
echo "Two of my employees are "
. $employee_array[0] . " & " . $employee_array[1];
echo "<br />Two more employees of mine are "
. $employee_array[2] . " & " . $employee_array[3];

//  O/P : Two of my employees are Bob & Sally
    Two more employees of mine are Charlie & Clare

PHP arrays are quite useful when used in conjunction with loops, which we will talk about in a later lesson. Above we showed an example of an array that made use of integers for the keys (a numerically indexed array). However, you can also specify a string as the key, which is referred to as an associative array.

Associative Arrays :

In an associative array a key is associated with a value. If you wanted to store the salaries of your employees in an array, a numerically indexed array would not be the best choice. Instead, we could use the employees names as the keys in our associative array, and the value would be their respective salary.

CODE/PROGRAM/EXAMPLE
//PHP Code:
$salaries["Bob"] = 2000;
$salaries["Sally"] = 4000;
$salaries["Charlie"] = 600;
$salaries["Clare"] = 0;
echo "Bob is being paid - $" . $salaries["Bob"] . "<br />";
echo "Sally is being paid - $" . $salaries["Sally"] . "<br />";
echo "Charlie is being paid - $" . $salaries["Charlie"] . "<br />";
echo "Clare is being paid - $" . $salaries["Clare"];

//  O/P : Bob is being paid - $2000
    Sally is being paid - $4000
    Charlie is being paid - $600
    Clare is being paid - $0

Multidimensional Arrays :

Elements in an array can also be an array, allowing for multidimensional arrays. An example, in accordance with the motoring examples above, is:

CODE/PROGRAM/EXAMPLE
<?php
$cars = array(
"car1" => array("make" => "Toyota","colour" => "Green","year" => 1999,"engine_cc" =>
1998),
"car2" => array("make" => "BMW","colour" => "RED","year" => 2005,"engine_cc" =>
2400),
"car3" => array("make" => "Renault","colour" => "White","year" => 1993,"engine_cc" =>
1395),
);
?>
CODE/PROGRAM/EXAMPLE
//In this example, if you were to use:
<?php
echo "$cars["car1"]["make"]<br>";
echo "$cars["car3"]["engine_cc"]";
?>

//  O/P : Toyota
    1395

Array Functions :

There exist dozens of array manipulation functions. Before implementing your own, make sure it doesn't already exist as a PHP function in Array functions (PHP manual entry).

Array traversal :

In various circumstances, you will need to visit every array element and perform a task upon it.

The simplest and the most widely used method for this is the foreach operator which loops through the whole array and works individually with each key/item couple. If a more complex way of traversing the array is needed, the following functions operate using the internal array pointer:

  • reset - sets the internal pointer to the first element and returns the first element
  • prev - sets the internal pointer to the previous element and returns it
  • current - returns the current element; does not change the internal pointer
  • next - sets the internal pointer to the next element and returns it
  • each - returns the current element; then sets the internal pointer to the next element
  • end - sets the internal pointer to the last element and returns the last element
CODE/PROGRAM/EXAMPLE
<?php
// using an arrays iterator to print its values in reverse order
$my_array = array("a", "b", "c");
end($my_array);
while($i = current($my_array)) {
echo $i."\n";
prev($my_array);
}
?>

Another possibility is defining a function and applying it to each array element via one of the following functions:

array_walk - applies a function to each array element array_walk_recursive - same, but if the element is itself an array, it will traverse that array too

#arrays_in_php #php_arrays #arrays_php #arrays_Syntax_in_php #array_function_method_in_php #Numerically_Indexed_Array_in_php #Associative_Arrays_in_php #Multidimensional_Arrays_in_php #Array_Functions_in_php #Array_traversal_in_php

(New page will open, for Comment)

Not yet commented...