Internet Technology
Internet Technology
INTRODUCTION
The term PHP is an acronym for – Hypertext Preprocessor. PHP is a
server-side scripting language designed specifically for web
development. It is open-source which means it is free to download and
use. It is very simple to learn and use. The file extension of PHP is
“.php”.
$name_two[0] = "ZACK";
$name_two[1] = "ANTHONY";
$name_two[2] = "RAM";
$name_two[3] = "SALIM";
$name_two[4] = "RAGHAV";
echo "Accessing the 2nd array elements directly:\n";
echo $name_two[2], "\n";
echo $name_two[0], "\n";
echo $name_two[4], "\n";
?>
Output:
Accessing the 1st array elements directly:
Ram
Zack
Raghav
Accessing the 2nd array elements directly:
RAM
ZACK
RAGHAV
Associative Arrays
These types of arrays are similar to the indexed arrays but instead of
linear storage, every value can be assigned with a user-defined key of
string type.
Examples of Associative Arrays
Example 1:
<?php
$name_one = array("Zack"=>"Zara", "Anthony"=>"Any",
"Ram"=>"Rani", "Salim"=>"Sara",
"Raghav"=>"Ravina");
$name_two["zack"] = "zara";
$name_two["anthony"] = "any";
$name_two["ram"] = "rani";
$name_two["salim"] = "sara";
$name_two["raghav"] = "ravina";
echo "Accessing the elements directly:\n";
echo $name_two["zack"], "\n";
echo $name_two["salim"], "\n";
echo $name_two["anthony"], "\n";
echo $name_one["Ram"], "\n";
echo $name_one["Raghav"], "\n";
?>
Output:
Accessing the elements directly:
zara
sara
any
Rani
Ravina
Output:
Looping using foreach:
Husband is Zack and Wife is Zara
Husband is Anthony and Wife is Any
Husband is Ram and Wife is Rani
Husband is Salim and Wife is Sara
Husband is Raghav and Wife is Ravina
Looping using for:
zack zara
anthony any
ram rani
salim sara
raghav ravina
Multidimensional Arrays
Multi-dimensional arrays are such arrays that store another array at each
index instead of a single element. In other words, we can define multi-
dimensional arrays as an array of arrays. As the name suggests, every
element in this array can be an array and they can also hold other sub-
arrays within. Arrays or sub-arrays in multidimensional arrays can be
accessed using multiple dimensions.
Examples of Multidimensional Arrays
Example 1:
<?php
$favorites = array(
array(
"name" => "Dave Punk",
"mob" => "5689741523",
"email" => "[email protected]",
),
array(
"name" => "Monty Smith",
"mob" => "2584369721",
"email" => "[email protected]",
),
array(
"name" => "John Flinch",
"mob" => "9875147536",
"email" => "[email protected]",
)
);
echo "Dave Punk email-id is: " . $favorites[0]["email"], "\n";
echo "John Flinch mobile number is: " . $favorites[2]["mob"];
?>
Output:
Dave Punk email-id is: [email protected]
John Flinch mobile number is: 9875147536
Example 2:
<?php
$favorites = array(
"Dave Punk" => array(
"mob" => "5689741523",
"email" => "[email protected]",
),
"Dave Punk" => array(
"mob" => "2584369721",
"email" => "[email protected]",
),
"John Flinch" => array(
"mob" => "9875147536",
"email" => "[email protected]",
)
);
$keys = array_keys($favorites);
for($i = 0; $i < count($favorites); $i++) {
echo $keys[$i] . "\n";
foreach($favorites[$keys[$i]] as $key => $value) {
echo $key . " : " . $value . "\n";
}
echo "\n";
}
?>
Output:
Dave Punk
mob : 2584369721
email : [email protected]
John Flinch
mob : 9875147536
email : [email protected]
2. array_pop()
Removes the last element from an array.
3. array_merge()
Merges one or more arrays into one.
4. array_diff()
Computes the difference between arrays.
REFERENCES
1. https://www.geeksforgeeks.org
2.https://www.academia.edu
3.https://iopscience.iop.org
4.https://ieeexplore.ieee.org