PHP-SingleDimention Array tutorial
Arrays in PHP
PART2
What is an array in PHP?
Array: It is a kind of variable used to store multiple values. Basically, an array is a data structure. A named memory location where all the values will be stored in a contiguous(next to each other) manner in the memory.
There are 3 types of arrays available in PHP.
1. Indexed / Single dimensional
2. Associative
3. Multidimensional
Advantages of Arrays
It is easy to perform tasks like accessing, adding, removing, modifying, sorting etc., since a group of values stored under a single name. This avoids the confusion of using multiple variables.
Disadvantages of Arrays
Arrays are fixed in size. Can store limited amount of data.
In this article, you are going to learn about how to create a single array with various types of data and print it.
1. Indexed/Single Array in PHP. :
- All the values will be stored in a single row.
- Each value in an array will be identified by its index.
- Array index starts with zero.
What you learn
1. Creating an empty array
ex:
$colors= array();
2. Creating array with string values
ex:
$colors= array("red", "green", "blue", "yellow");
Adding /Modifying Array Elements
echo "<br>". $colors[0];
$colors[4]="orange";
echo "<br>". $colors[4] ;
$colors[3]="pink";
echo "<br>". $colors[3] . "<br>";
code example
singlearray.php
<?php
$colors= array("red", "green", "blue", "yellow");
//Adding /Modifying Array Elements
echo "<br>". $colors[0]; $colors[4]="orange";
echo "<br>". $colors[4] ; $colors[3]="pink";
echo "<br>". $colors[3] . "<br>";
?>
Result:
red
orange
pink
To print complete array use:
print_r($colors);
ex:
<?php
$colors= array("red", "green", "blue", "yellow");
print_r($colors);
?>
Result:
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
Some more example on single array
ex2:
$browsers = array("Chrome", "FireFox", "IE");
ex3:
$cart_items = array("HeadPhone", "PenDrive", "Speakers");
ex5:
$dayss = array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat" , "Sun");
3. Creating array with numbers
ex:
numeric array
$numbers = array(10, 20, 30, 40);
numaric-array.php
<?php
$numbers = array(10, 20, 30, 40);
print $numbers [0]. "<br>" ;
print $numbers [1]. "<br>" ; ?>
Result:
10
20
Default position(index) of each value in an array is as shown below
index values
[0] 10
[1] 20
[2] 30
[3] 40
At 0 position value 10 is stored.
At position 1, value 20 is stored… and so on.
Practice :
→Print all the array values with echo.
2. Create a vehicles array with ("Truck","Bus","Car","Auto","Bicycle");
→Add ship at the end of the array and print complete array
→Replace Bus with Rickshaw.
3. Create an animals array and store five animals, and print it.
5. Create a fruites array with 5 fruits and print array in reverse using echo.
6. Create a products array with given list of items and print only 3rd element from the array
chair
desks
bench
stool
tables
cabinet
4. Creating array with mixed type
An array in PHP can have values of different type
such as strings, numbers, boolean etc.,
ex:
$student_details = array("John", 279, "PHP", TRUE);
mixedtype-array.php
<?php
$student_details = array("John", 279, "PHP", TRUE);
print "<br>Name:" . $student_details [0];
print "<br>Score:" . $student_details [1];
print "<br>Course:" . $student_details [2];
print "<br>Qualified:" . $student_details [3];
?>
Result:
Name:John
Score:279
Course:PHP
Qualified:1
Inplace of TRUE if FALSE is written, then the result is as below:
Name:John
Score:279
Course:PHP
Qualified:
Note : False returns empty. True returns 1 in PHP
5. Processing Arrays with Loops
Using for Loop:
ex:
forloop-array-example.php
<?php $colors= array("red", "green", "blue", "yellow"); for ($i= 0; $i< 4; $i++)
{
print $colors[$i]."<br />";
}
?>
Result:
red
green
blue
yellow
Using for each Loop:
for-each loop used with arrays, it iterates through the pre-defined set of values.
This loop takes two arguments. On e is array. And another one is, temporary variable.
In each iteration, it reads one value from an array and put it in temp variable. Inside the loop, that temp value will be used.
This loop traverse from the beginning of the array to the end of the array.
Syntax:
foreach ($arrayname as $tempvar)
{
print $tempvar;
}
ex:
$numbers = array(1, 2, 3, 4);
foreach ($numbers as $num)
{
print $num;
}
program:
foreach-array-example.php
<?php $numbers = array(1, 2, 3, 4);
foreach ($numbers as $num)
{
print $num;
}
?>
Result:
1234
You can follow the PART2 of PHP Arrays
Comments
Post a Comment