Internet and Web Programming - CSE3002 Digital Assignment - 1
Internet and Web Programming - CSE3002 Digital Assignment - 1
17BCE2411
Short Notes:
Arrays in php serve as a data structure to store data. This data can be of both similar or
dissimilar types of variables of data. This implies that the data stored in an array can be a
combination of various different data types, that is, strings, integers, floats, etc. and can also be
something like 100 integers.
Arrays are usually used to avoid the headache of using lots of different variables for every
little value, or when a linear format is required, in which case indexed arrays are used. Arrays in
php are usually divided into 3 different categories:
• indexed arrays
• associative arrays
• multidimensional arrays
Indexed arrays: These are linearly stored values, that is, they occur in a sequence or linear order.
Each element in this type of array has a corresponding index associated with it, starting from index
0. Indexed arrays kind of work like associative arrays with the linear values of indices functioning
as the keys. To retrieve a particular element from this array, the name of the array followed by the
index number in square brackets is used. Eg: arr1[0] fetches the first element.
Associative arrays: These arrays work like dictionaries in python. The elements are stored in key-
value pairs and not in any particular order. The value for any element can be accessed using its key.
Eg: arr1[‘key1’] fetches ‘value1’, the value corresponding to that key.
Multidimensional arrays: These arrays function as nested structures of arrays within arrays. This
could be indexed arrays within indexed arrays, or a combination of indexed and associative arrays,
etc. to create a multidimensional data structure. To retrieve an element, the indices or keys are used
in consecutive order, from the lower dimension to the higher. Eg: arr1[0][2] fetches the 3rd element
from the 1st row of the matrix.
Integrated php array demonstration website:
Screenshots:
Program Code (3 files):
1) da1.php
<?php
session_start();
if($_POST){
$_SESSION['numel'] = $_POST['numel'];
$selec = $_POST['selector'];
if($selec=='Indexed'){
header("Location:indexarr.php");
exit;
}
else{
header("Location:assoc.php");
exit;
}
?>
<html>
<head>
<title>
IWP_DA1
</title>
</head>
<body>
<div align = "center" style = "align-content: center; border-style: solid; border-width = 5px;
border-color: blue; margin-left: 500px; margin-right: 500px; ">
<form action = "<?php echo $_SERVER["PHP_SELF"];?>" method = "post" name =
"yolo">
<caption><b>Harshil Shah - 17BCE2411</b></caption><br><br>
<caption><i>IWP DA1 - Arrays in PHP:</i></caption><br><br>
<input type = 'number' name = 'numel' placeholder = 'Enter the number of
elements:'><br><br>
<label>Select the array type: </label>
<select name = 'selector'><option value = 'Indexed'
selected="true">Indexed</option><option value = 'Associative'>Associative</option></select>
<br><br><input type = 'submit' name = 'indxarr' value = 'Begin array
creation'>
</form>
</div>
</body>
2) indexarr.php
<?php
session_start();
$arr1 = array();
$numel = $_SESSION['numel'];
echo "<div align = \"center\" style = \"align-content: center; border-style: solid; border-width = 5px;
border-color: blue; margin-left: 400px; margin-right: 500px; \">";
echo "<h3>Created sample indexed array displayed using print_r function: </h3><br><br>";
array_push($arr1,"item".strval($i));
}
if(isset($_POST['sortasc'])){
echo "Sorted array by displayed as a string: ";
asort($arr1);
}
if(isset($_POST['sortdesc'])){
echo "Reverse sorted array displayed as a string: ";
arsort($arr1);
}
if(isset($_POST['slice'])){
echo "Array sliced from 2nd index displayed as a string: ";
$arr1 = array_slice($arr1,2);
}
if(isset($_POST['splice'])){
echo "Array spliced from 2nd index displayed as a string: ";
$arr1 = array_splice($arr1, 2);
}
echo "<br><br>";
print_r($arr1);
echo "<br><br><br></div>";
?>
<html>
<head>
<title>
IWP_DA1
</title>
</head>
<body>
<div align = "center" style = "align-content: center; border-style: solid; border-width = 2px;
border-color: blue; margin-left: 400px; margin-right: 500px; ">
<br><br>
<form name = 'assoc' method = "post" action = "<?php echo $_SERVER['PHP_SELF'];?>">
<button name = 'sortasc'>Sort Ascending</button>
<button name = 'sortdesc'>Sort Descending</button>
<button name = 'slice'>Slice Array</button>
<button name = 'splice'>Splice Array</button>
</form>
</div>
</body>
3) assoc.php
<?php
session_start();
$numel = $_SESSION['numel'];
echo "<div align = \"center\" style = \"align-content: center; border-style: solid; border-width = 5px;
border-color: blue; margin-left: 400px; margin-right: 500px; \">";
echo "<h3>Created sample associative array displayed using print_r function: </h3><br><br>";
for( $i = 0; $i<$numel; $i++ ) {
$key = "key".strval($i);
$val = "value".strval($i);
$arr1[$key] = $val;
}
if(isset($_POST['sortasc'])){
echo "Sorted array by key displayed as a string: ";
ksort($arr1);
}
if(isset($_POST['sortdesc'])){
echo "Reverse sorted array by key displayed as a string: ";
krsort($arr1);
}
if(isset($_POST['slice'])){
echo "Array sliced from 2nd index displayed as a string: ";
$arr1 = array_slice($arr1,2);
}
if(isset($_POST['splice'])){
echo "Array spliced from 2nd index displayed as a string: ";
$arr1 = array_splice($arr1, 2);
}
echo "<br><br>";
print_r($arr1);
echo "<br><br><br></div>";
?>
<html>
<head>
<title>
IWP_DA1
</title>
</head>
<body>
<div align = "center" style = "align-content: center; border-style: solid; border-width = 2px;
border-color: blue; margin-left: 400px; margin-right: 500px; ">
<br><br>
<form name = 'assoc' method = "post" action = "<?php echo $_SERVER['PHP_SELF'];?>">
<button name = 'sortasc'>Sort Ascending</button>
<button name = 'sortdesc'>Sort Descending</button>
<button name = 'slice'>Slice Array</button>
<button name = 'splice'>Splice Array</button>
</form>
</div>
</body>
********************************************************************************