UNIT – II Chapter 4 - Array - Copy
UNIT – II Chapter 4 - Array - Copy
In this line of code, an array of four elements is created, with each element containing a string value. The array is
then assigned to the variable $authors.
You can now access any of the array elements via the single variable name, $authors. This array is an indexed array,
which means that each of the array elements is accessed via its own numeric index, starting at zero.
Here, the “Steinbeck” element has an index of 0 , “ Kafka ” has an index of 1 , “ Tolkien ” has an index of 2 , and “
Dickens ” has an index of 3 .
If you want to create an associative array, where each element is identified by a string index rather than a number,
you need to use the => operator, as follows:
$myBook = array( “title” = > “The Grapes of Wrath”, “author” = > “John Steinbeck”, “pubYear” = > 1939 );
This creates an array with three elements: “ The Grapes of Wrath ” , which has an index of “ title “ ; “ John
Steinbeck ”, which has an index of “ author “ ; and 1939 , which has an index of “ pubYear ”.
In other words, you write the variable name, followed by the index of the element in square brackets. If you want to
access the elements of an associative array, simply use string indices rather than numbers:
$myBook = array( “title” = > “The Grapes of Wrath”,
“author” = > “John Steinbeck”,
“pubYear” = > 1939 );
$myTitle = $myBook[“title”]; // $myTitle contains “The Grapes of Wrath”
$myAuthor = $myBook[“author”]; // $myAuthor contains “Steinbeck”
You don’t have to use literal values within the square brackets; you can use any expression, as long as it
evaluates to an integer or string as appropriate:
$authors = array( “Steinbeck”, “Kafka”, “Tolkien”, “Dickens” );
$pos = 2;
echo $authors[$pos + 1]; // Displays “Dickens”
Changing Elements
In addition to accessing the array values you can change values using the same techniques.
For example, the following code changes the value of the third element in an indexed array from “ Tolkien ” to “
Melville “ :
What if you wanted to add a fifth author? You can just create a new element with an index of 4,
as follows:
There’s an even easier way to add a new element to an array — simply use square brackets with no index:
When you do this, PHP knows that you want to add a new element to the end of the array, and it automatically
assigns the next available index — in this case, 4 — to the element.
In fact, you can create an array from scratch simply by creating its elements using the square bracket syntax. The
following three examples all produce exactly the same array:
However, just as with regular variables, you should make sure your arrays are initialized properly first. In the second
and third examples, if the $authors2 or $authors3 array variables already existed and contained other elements, the
final arrays might end up containing more than just the four elements you assigned.
$authors = array();
$authors[] = “Steinbeck”;
$authors[] = “Kafka”;
$authors[] = “Tolkien”;
$authors[] = “Dickens”;
You can also add and change elements of associative arrays using square bracket syntax. Here an associative array is
populated in two ways: first using the array() construct, and second using the
square bracket syntax:
$variable: This parameter specifies the variable to be printed and is a mandatory parameter.
$isStore: This an option parameter. This parameter is of boolean type whose default value is FALSE and is
used to store the output of the print_r() function in a variable rather than printing it. If this parameter is set
to TRUE then the print_r() function will return the output which it is supposed to print.
<?php
// string variable
$var1 = "Welcome to IT";
// integer variable
$var2 = 92;
// array variable
$arr = array('0' => "Welcome", '1' => "to", '2' => "IT");
?>
<?php
# PHP program to illustrate the print_r() function when $isStore is set to true
# array variable
$arr = array('0' => "Welcome", '1' => "to", '2' => "IT");
echo $results;
?>
<?php
// Input array
$array = array("Rajesh", "krishna", "Rahul",
"Nandu", "Bhairav");
?>
count():
The count() function returns the number of elements in an array.
<?php
$cars=array("Volvo","BMW","Toyota");
echo count($cars);
?>
Numeric Array:
These arrays can store numbers, strings and any object but their index will be prepresented by numbers. By default,
array index starts from zero.
Example:
Following is the example showing how to create and access numeric arrays. Here we have used array() function to
create array.
<html>
<body>
<?php
/* First method to create array. */
$numbers = array( 1, 2, 3, 4, 5);
foreach( $numbers as $value )
{
echo "Value is $value <br />";
}
Associative Arrays:
The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of
their index. Associative array will have their index as string so that you can establish a strong association between
key and values.
NOTE: Don't keep associative array inside double quote while printing otherwise it would not return any value.
Example:
<html>
<body>
<?php
);
$salaries['mohammad'] = "high";
$salaries['qadir'] = "medium";
$salaries['zara'] = "low";
?>
</body>
</html>
Multidimensional Arrays:
A multi-dimensional array each element in the main array can also be an array. And each element in the sub-array
can be an array, and so on. Values in the multi-dimensional array are accessed using multiple index.
Example
In this example we create a two dimensional array to store marks of three students in three subjects:
This example is an associative array, you can create numeric array in the same fashion.
<html>
<body>
<?php
$marks = array(
"chemistry" => 39
),
"chemistry" => 29
),
"chemistry" => 39
);
</body>
</html>
The simplest way to use foreach is to retrieve each element’s value, as follows:
As you might imagine, the foreach loop continues to iterate until it has retrieved all the values in the array, from the
first element to the last. On each pass through the loop, the $value variable gets set to the value of the current
element. You can then do whatever you need to do with the value within the loop’s code block. Then, the loop
repeats again, getting the next value in the array, and so on.
Example:
<?php
?>
Steinbeck
Kafka
Tolkien
Dickens
You can use any variable name you like to store the value. Essentially, any variable that you place after the as in the
foreach statement gets assigned the current element’s value.
To use foreach to retrieve both keys and values, use the following syntax:
This behaves exactly like the previous foreach construct; the only difference is that the element’s key is also stored
in the $key variable. (Again, you can use any variable names you like; they don ’ t have to be
<?php
$aso_arr = array(
"Up"=>"North",
"Down"=>"South",
"Left"=>"West",
"Right"=>"East"
);
#Use foreach loop to traverse each elements of array and display its key and value
foreach($aso_arr as $side=>$direc) {
?>