Declaration and displaying array in php example- An array is a structured
data type that is useful for storing a number of data of the same type.
The parts that make up the array are called array elements, each of
which is individually accessible through the index array. The array
index can be either an integer or a string.
To better understand the application of arrays, here's how to Declare and Show Array on a php program.
Program 1
File Name: array01.php
Description: Program declares array with numeric index.
Put the following code into notepad.
<?php
$arrFruit = array ("Mango", "Apple", "Banana", "Orange");
echo $arrFruit[0]; //Mango
echo $arrFruit[3]; //Orange
$arrColor = array();
$arrColor[] = "Red";
$arrColor[] = "Blue";
$arrColor[] = "Green";
$arrColor[] = "White";
echo $arrColor[0]; //Red
echo $arrColor[2]; //Green
?>
Save the code in the htdocs folder named array01.php.
Program Explanation 1
To declare or define an array in PHP can use keyword array (). In program 1, there are declarations and definitions of arrays on line 3. The number of array elements need not be mentioned during declaration. As for displaying the contents of an array on a particular element, simply by naming the array and its index array (see row 4 and 5). Declaring and filling the array can also be done as in the 7th row to the 11th row. By defining the array as in the above two ways, the index (key) array will automatically be filled with integer integers starting from 0.
To see the results, open the browser type http://localhost/array01.php in the address bar then enter.
Program 2
File Name: array02.php
Description: The program declares an array with an index string (an associative array).
Open notepad, type the following php code into notepad.
<?php
$arrValue = array ("Johans" => 80, "Otim" => 90, "Jessica" => 75,
"Michael" => 85);
echo $arrValue['Johans']; //80
echo $arrValue['Otim']; //90
$arrValue = array();
$arrValue['Milka'] = 80;
$arrValue['Benny'] = 95;
$arrValue['Selly'] = 77;
echo $arrValue['Benny']; //95
echo $arrValue['Milka']; //80
?>
Save the php code in the htdocs folder named array02.php.
Program Explanation 2
To declare or define an associative array (an array that uses an index other than an integer), it can be by name the index first followed by operator => and followed by the value or element value. See the 3rd row program! As for displaying the value or value of an array, it could be by name of the array followed by its index array (see row 4 and 5). The definition of an associative array can also be done as in the 7th row to the 10th row.
To run the array program, open the browser, in the address bar type http://localhost/array02.php then enter.
Program 3
File Name: array03.php
Description: The program displays the entire contents of an array with a numeric index.
Type the following code in notepad.
<?php
$arrColor = array ("Blue","Black","Red","Yellow","Green");
echo "Displays the contents of the array with for : <br>";
for ($i=0; $i<count($arrColor); $i++) {
echo "Do you like <font color=$arrColor[$i]>". $arrColor[$i]
."</font> ?<br>";
}
echo "<br>Displays the contents of an array with foreach : <br>";
foreach ($arrColor as $color) {
echo "Do you like <font color=$color>". $color ."</font>
?<br>";
}
?>
Save the code into the htdocs folder named array03.php
Program Explanation 3
To display the entire contents of the array can be done by looping for, while, do..while or foreach. Lines 5 - 7 of the above programs are examples of displaying arrays with the for loop. The count() function on line 5 serves to find the number of elements of the $arrColor array. To display an array can also use the structure foreach(). The foreach structure will take the array elements from the pointer position to the end of the pointer. In the example of the above program (line 10) can be declared with the statement that "for each array element $arrColor, insert into variable $color". Display program 3 can be seen in Figure 3.
If you want to see the results, open the browser, type http://localhost/array03.php in the address bar then enter. The result will look like the following.
Program 4
File Name: array04.php
Description: The program displays the entire contents of an associative array.
Type the following php code into notepad.
<?php
$arrValue = array ("Johans" => 80, "Otim" => 90, "Jessica" => 75,
"Michael" => 85);
echo "Displays the contents of an array with foreach : <br>";
foreach ($arrValue as $name => $value) {
echo "Value $name = $value<br>";
}
reset ($arrValue);
echo "<br>Displays the contents of an array with a while and list : <br>";
while (list ($name, $value) = each($arrValue)) {
echo "Value $name = $value<br>";
}
?>
Save the code with the name array04.php in the htdocs folder.
Program Explanation 4
To display an associative array can use foreach and while loops with the list() and each() functions. The 5th row of program 4. above can be expressed by the statement "for each array element $arrValue, enter its key / index into variable $name and enter its value into variable $value". On line 8 there is a function reset() function to return the array pointer to the first element. On line 10, an associative array is displayed with a while structure that uses the list() and each() function.
If you want to display the results, open the browser then enter the address http://localhost/array04.php in the address bar, then enter. The result will look like the following.
Program 5
File Name: array05.php
Description: The program prints the array structure.
Open notepad, then type this php code.
<?php
$arrColor = array ("Blue","Black","Red","Yellow","Green");
$arrValue = array ("Johans" => 80, "Otim" => 90, "Jessica" => 75, "Michael" => 85);
echo "<pre>";
print_r ($arrColor);
echo "<br>";
print_r ($arrValue);
echo "</pre>";
?>
Save the code with the name array05.php in the htdocs folder.
Program Explanation 5
In program 5 above there is the function print_r() (line 5) that serves to display the structure of the array. Display program 5 can be seen in figure 5.
To see the results, open the browser then in the address bar type http://localhost/array05.php then enter.
That's the explanation from me about Declaration and Displaying Array in PHP Example, hopefully can increase our knowledge in learning php programming language. Learn also articles about How to Get Input From Textarea in PHP.
To better understand the application of arrays, here's how to Declare and Show Array on a php program.
Program 1
File Name: array01.php
Description: Program declares array with numeric index.
Put the following code into notepad.
<?php
$arrFruit = array ("Mango", "Apple", "Banana", "Orange");
echo $arrFruit[0]; //Mango
echo $arrFruit[3]; //Orange
$arrColor = array();
$arrColor[] = "Red";
$arrColor[] = "Blue";
$arrColor[] = "Green";
$arrColor[] = "White";
echo $arrColor[0]; //Red
echo $arrColor[2]; //Green
?>
Save the code in the htdocs folder named array01.php.
Program Explanation 1
To declare or define an array in PHP can use keyword array (). In program 1, there are declarations and definitions of arrays on line 3. The number of array elements need not be mentioned during declaration. As for displaying the contents of an array on a particular element, simply by naming the array and its index array (see row 4 and 5). Declaring and filling the array can also be done as in the 7th row to the 11th row. By defining the array as in the above two ways, the index (key) array will automatically be filled with integer integers starting from 0.
To see the results, open the browser type http://localhost/array01.php in the address bar then enter.
Program 2
File Name: array02.php
Description: The program declares an array with an index string (an associative array).
Open notepad, type the following php code into notepad.
<?php
$arrValue = array ("Johans" => 80, "Otim" => 90, "Jessica" => 75,
"Michael" => 85);
echo $arrValue['Johans']; //80
echo $arrValue['Otim']; //90
$arrValue = array();
$arrValue['Milka'] = 80;
$arrValue['Benny'] = 95;
$arrValue['Selly'] = 77;
echo $arrValue['Benny']; //95
echo $arrValue['Milka']; //80
?>
Save the php code in the htdocs folder named array02.php.
Program Explanation 2
To declare or define an associative array (an array that uses an index other than an integer), it can be by name the index first followed by operator => and followed by the value or element value. See the 3rd row program! As for displaying the value or value of an array, it could be by name of the array followed by its index array (see row 4 and 5). The definition of an associative array can also be done as in the 7th row to the 10th row.
To run the array program, open the browser, in the address bar type http://localhost/array02.php then enter.
Program 3
File Name: array03.php
Description: The program displays the entire contents of an array with a numeric index.
Type the following code in notepad.
<?php
$arrColor = array ("Blue","Black","Red","Yellow","Green");
echo "Displays the contents of the array with for : <br>";
for ($i=0; $i<count($arrColor); $i++) {
echo "Do you like <font color=$arrColor[$i]>". $arrColor[$i]
."</font> ?<br>";
}
echo "<br>Displays the contents of an array with foreach : <br>";
foreach ($arrColor as $color) {
echo "Do you like <font color=$color>". $color ."</font>
?<br>";
}
?>
Save the code into the htdocs folder named array03.php
Program Explanation 3
To display the entire contents of the array can be done by looping for, while, do..while or foreach. Lines 5 - 7 of the above programs are examples of displaying arrays with the for loop. The count() function on line 5 serves to find the number of elements of the $arrColor array. To display an array can also use the structure foreach(). The foreach structure will take the array elements from the pointer position to the end of the pointer. In the example of the above program (line 10) can be declared with the statement that "for each array element $arrColor, insert into variable $color". Display program 3 can be seen in Figure 3.
If you want to see the results, open the browser, type http://localhost/array03.php in the address bar then enter. The result will look like the following.
Program 4
File Name: array04.php
Description: The program displays the entire contents of an associative array.
Type the following php code into notepad.
<?php
$arrValue = array ("Johans" => 80, "Otim" => 90, "Jessica" => 75,
"Michael" => 85);
echo "Displays the contents of an array with foreach : <br>";
foreach ($arrValue as $name => $value) {
echo "Value $name = $value<br>";
}
reset ($arrValue);
echo "<br>Displays the contents of an array with a while and list : <br>";
while (list ($name, $value) = each($arrValue)) {
echo "Value $name = $value<br>";
}
?>
Save the code with the name array04.php in the htdocs folder.
Program Explanation 4
To display an associative array can use foreach and while loops with the list() and each() functions. The 5th row of program 4. above can be expressed by the statement "for each array element $arrValue, enter its key / index into variable $name and enter its value into variable $value". On line 8 there is a function reset() function to return the array pointer to the first element. On line 10, an associative array is displayed with a while structure that uses the list() and each() function.
If you want to display the results, open the browser then enter the address http://localhost/array04.php in the address bar, then enter. The result will look like the following.
Program 5
File Name: array05.php
Description: The program prints the array structure.
Open notepad, then type this php code.
<?php
$arrColor = array ("Blue","Black","Red","Yellow","Green");
$arrValue = array ("Johans" => 80, "Otim" => 90, "Jessica" => 75, "Michael" => 85);
echo "<pre>";
print_r ($arrColor);
echo "<br>";
print_r ($arrValue);
echo "</pre>";
?>
Save the code with the name array05.php in the htdocs folder.
Program Explanation 5
In program 5 above there is the function print_r() (line 5) that serves to display the structure of the array. Display program 5 can be seen in figure 5.
To see the results, open the browser then in the address bar type http://localhost/array05.php then enter.
That's the explanation from me about Declaration and Displaying Array in PHP Example, hopefully can increase our knowledge in learning php programming language. Learn also articles about How to Get Input From Textarea in PHP.
Comments
Post a Comment