PHP FormPart-12


 

PHP FormPart-12

Method Two ? Assign values to an array: There is also another mode to assign values into an array. Such as :

Method Two ? Assign values to an array: There is also another mode to assign values into an array. Such as :

Topic : HTML FORM

Part - 11: Two basic methods to put values into an array. 

Method One ? Value between the round brackets:

In the first method, putting the values in between the parenthesis or array. Here, we are going to give the name to the array is $days. Between the parenthesis array() we have to put our values between the double codes and separated by the comma.

 Let's see the example to understand this method in a better way :

$days = array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

Arrays work according to the position. It always start from the 0 instead of 1. In the above code, "Sunday" is in position zero, "Monday" is in position 1, "Tuesday" is in position 2, "Wednesday" is in position 3, "Thursday" is in position 4, "Friday" is in position 5, "Saturday" is in position 6.

The position is known as key. The value is attached with the key and we specify our choice of numbers for the keys. 

Let's see the example below :

$days = array(1 => "Sunday", 2 => "Monday", 3 => "Tuesday", 4 => "Wednesday",  5 => "Thursday", 6 => "Friday", 7 => "Saturday");

Here we typed the number for our keys, along with equals sign and a right angle bracket (=>). If you noticed that the first key is changed from 0 to 1 and values are stored under key 1 as "Sunday". We should always keep in mind the commas in between two keys. If you forget to mentioned the keys, it will show an error message on your screen. 

Let's see the Keys and values that are set up in the array above :

1=> "Sunday",
2=> "Monday",
3=> "Tuesday",
4=> "Wednesday"

5=> "Thursday",
6=> "Friday",
7=> "Saturday"

If the keys are set by the PHP himself, then it will look like this:

0=> "Sunday",
1=> "Monday",
2=> "Tuesday",
3=> "Wednesday"

4=> "Thursday",
5=> "Friday",
6=> "Saturday"

We can store numbers and text and numbers together.

 Let's see the both the example without setting up any keys :

Example 1 :

$numbers = array("100", "200", "300", "400",  "500", "600", "700");

The output of the above example is : 

0=> "100",
1=> "200",
2=> "300",
3=> "400"

4=> "500",
5=> "600",
6=> "700"

Example 2 :

$numbers = array( "100", "Sunday", "300", "Monday",  "500", "Tuesday", "700");

The output of the above example is : 

0=> "100",
1=> "Sunday",
2=> "300",
3=> "Monday"

4=> "500",
5=> "Tuesday",
6=> "700"

You can also assign your own keys. In the next part of this tutorial we will learn the second method to put values into an array. 

Ads