PHP Arrays

The array is a different variable that can store various values.
In other words, The array is a special type of variable that can contain various variables. The array can contain a lot of values in a list.

Definition and use of Array

The array is the collection of similar data items. The array is used for the same data types.
Array a kind of data structure that can store a fixed-size sequential collection of elements of the same data type
. Array removes the data complexity.
The array() function can contain multiple values.


Syntax of Array


array("value1", "value2 ", "value3");

In the above syntax of an array, we have declared an array() function with multiples values. The array can be defined as the database structure for values. You can store values in a list .In above syntax , you can see an array() function contained value1 , value 2 and value 3.

Examples of Array

In these examples, we will declare the array function.

Example


<?php
$fruits = array("Banana", "Mango ", "Orange");
echo "I like " . $fruits[0] . ", " . $fruits[1] . " and " . $fruits[2] . ".";
?>
		
		
Run

In the above example, we declared an array of fruits variable. We used some of the fruits items as values. The index number is must in array. The index number initialized from the 0. i.e - array("Banana(0 index value )", "Mango(1 index value ) ", "Orange(2 index value )"); Banana is stored at 0 index value , manago is stored at 1 index value and orange is stored at index numer 2.

Sometimes we get out of our mind that the array index number starts from 0. If you think the array starts with an index number 1, then this is absolutely wrong. Keep in mind that the array index number starts from number 0. If you get an incorrect array index number, you will get an error. Array index always starts from 0. $fruits[0] ----- Zero index number of an array .

$fruits[1] ----- Index number one of an array .

$fruits[2] ----- Index number two of an array .

If you have a list of items (a list of fruit names, for example), store the fruits in single variables could look like this:

$fruits[0]=”Banana”;

$fruits[1]=”Mango”;

$fruits[2]=”Orange”;



Let's have another array example. In this example, we will declare the array one by one with the index number and its values. It's another way to declare array and store in a variable. It is known as an index array. We can define the index value one by one and print them like below example.

Example

<?php 
$x[0]=12;
$x[1]=14;
$x[2]=777;
$x[3]=44;
?>

Sr No <?php echo"$x[0]"; ?>

Sr No <?php echo "$x[1]"; ?>

Sr No <?php echo "$x[2]"; ?>

Sr No <?php echo "$x[3]"; ?>

Run

In the above array example, we declared the array one by one. The $x is an array variable or name. In the first example, we used the variable once and we did not give any array index number but in this example, we have defined different values with its index number. So this is different from the first example.

<?php 
   $odd_no= [1,3,5,7,9];
$first_odd_number = $odd_no[0];
$second_odd_number = $odd_no[1];
$third_odd_number = $odd_no[2];
$fourth_odd_number = $odd_no[3];

echo "This is  first odd number = $first_odd_number";
echo "
"; echo "This is second odd number = $second_odd_number"; echo "
"; echo "This is third odd number = $third_odd_number"; echo "
"; echo "This is fourth odd number = $fourth_odd_number"; ?>
Output-
This is first odd number = 1
This is second odd number = 3
This is third odd number = 5
This is fourth odd number = 7

In this above example, we created an array which has taken the odd number. As you know, the odd numbers start 1,3,5,7,9, etc. An odd number is the meaning of which does not have to be completely divided by 2. Odd number divides by same odd number. i.e 9 divides by 9 and 1 divides by 1 . After that, we have stored different index values of the array in another variable. The variables are - 1.$first_odd_number - First variable stored 0 index value

2.$second_odd_number -second variable stored 0 index value

3.$third_odd_number -Third variable stored 0 index value

4.$fourth_odd_number- Fourth variable stored 0 index value

After that, we have printed the One by One Array index value.

<?php 
$num= [1,2,3,4];
$num2=[6,7,4];

echo $num[0]; 
echo "
"; echo $num2[0]; ?>
output-
 1
 6 

In this above example, we have made 2 Arrays. First Array has values 1,2,3, 4, and the other array has values 6,7,4. If you want to merge two arrays then you should use the array merge function which will merge these two arrays into an array.

There are three types of arrays.

1. Index Array .

PHP index starts from 0. We can store number, string, an object in the PHP array. All array elements assigned to an index number by default.

Learn Index Array

2.Two dimensional array .

Two dimensional array have rows and columns two-dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional integer array of size [x][y].

Learn Two dimensional Array

3.Associative Array

An associative array is one of the most reliable array in PHP. In an associative array, we can associate the name with each array of elements in PHP using the => symbol.

Learn Associative Array

Recommended Posts:-

Previous Posts:-