0% found this document useful (0 votes)
31 views11 pages

Practical No 04 PHP

The document contains multiple PHP code snippets demonstrating the creation, modification, and manipulation of arrays. It covers various array operations such as accessing elements, extracting variables, sorting, and merging arrays. Additionally, it includes examples of using associative arrays and functions like implode and explode.

Uploaded by

zubiyaansari417
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views11 pages

Practical No 04 PHP

The document contains multiple PHP code snippets demonstrating the creation, modification, and manipulation of arrays. It covers various array operations such as accessing elements, extracting variables, sorting, and merging arrays. Additionally, it includes examples of using associative arrays and functions like implode and explode.

Uploaded by

zubiyaansari417
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

PRACTICAL NO: 04

1. <html><head><title>Array</title></head>
<body>
<h1>Creating an Array</h1>
<?php
$course[0] = "Computer Engg.";
$course[1] = "Information Tech.";
$course[2] = "Electronics and
Telecomm.";
// Accessing the elements directly
echo $course[2] . "<br>";
echo $course[0] . "<br>";
echo $course[1] . "<br>";
?></body></html>

2. <html>
<head><title>аггау</title></head>
<body>
<h1> Creating an array </h1>
<?php
$course = array(0 => "Computer Engg.", 1 => "Information Tech.", 2 =>
"Electronics and Telecomm.");
echo $course [1];
?></body></html>
<html><head><title>Array</title></head>
<body>
<h1>Creating an array </h1>
<?php
$course = array(1 => "Computer Engg.",
5 => "Information Tech.",
3 => "Electronics and Telecomm.");
echo $course [5];
?></body></html>

3. <html><head><title>array</title></head>
<body>
<h1> Creating an array </h1>
<?php
$course = array("CO"=>549,
"IF"=>450,
"EJ"=>100);
echo "course['CO']=",
$course["CO"],"<br>";
echo "course['IF']=", $course["IF"],"<br>";
echo "course['EJ']=", $course["EJ"]."<br>";
?></body></html>
4.
<?php
$person = array(
array(
"name" => "Yogita K",
"mob" => "5689741523",
"email" => "[email protected]",
),
array(
"name" => "Manisha P.",
"mob" => "2584369721",
"email" => "[email protected]",
),
array(
"name" => "Vijay Patil",
"mob" => "9875147536",
"email" =>
"[email protected]",
)
);
echo "Manisha P's email-id is: " . $person[1]["email"] . "<br>";
echo "Vijay Patil's mobile no: " . $person[2]["mob"];
?>
5.
<?php
$mobile = array(
array("LG", 20, 18),
array("Sony", 30, 13),
array("Redmi", 10, 2),
array("Samsung", 40, 15)
);
echo $mobile[0][0] . ": In stock: " . $mobile[0][1] . ", sold: " . $mobile[0][2] .
".<br>";
echo $mobile[1][0] . ": In stock: " . $mobile[1][1] . ", sold: " . $mobile[1][2] .
".<br>";
echo $mobile[2][0] . ": In stock: " . $mobile[2][1] . ", sold: " . $mobile[2][2] .
".<br>";
echo $mobile[3][0] . ": In stock: " . $mobile[3][1] . ", sold: " . $mobile[3][2] .
".<br>";
?>
6.
<?php
$mobile = array
(
array("LG",20,18),
array("sony", 30,13),
array("Redme",10,2),
array("Samsung",40,15)
);
for ($row = 0; $row < 4; $row++)
{ echo "<p><b>Row number
$row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++)
{
echo "<li>".$mobile[$row][$col]."</li>";
}
echo "</ul>";
}?>
7.
<html><head><title>Array</title></head>
<body>
<h3> Extracting variables from array </h3>
<?php
$course["CO"]="Computer Engg.";
$course["IF"]="Information Tech.";
$course["EJ"]="Electronics and
Telecomm.";
extract($course);
echo "CO=$CO<BR>";
echo "IF=$IF<BR>";
echo "EJ=$EJ<BR>";
?></body></html>
8.
<html><head><title>Array</title></head>
<body>
<h3> Extracting variables from array
</h3>
<?php
$course[0]="Computer Engg.";
$course[1]="Information Tech.";
$course[2]="Electronics and Telecomm.";
list($one,$two)=$course;
echo $one,"<BR>";
echo $two; ?>
<?php
$var1="PHP";
$var2="JAVA";
$var3=compact("var1", "var2");
print_r($var3); ?>

9.
<html><head><title>array</title></head>
<body>
<h3> Extracting variables from array </h3>
<?php
$course[0]="Computer Engg.";
$course[1]="Information Tech.";
$course[2]="Electronics and
Telecomm.";
$text=implode(",",$course);
echo $text;
?></body></html>
10.
<?php
// PHP Code to implement join function
$InputArray = array('CO', 'IF', 'EJ');
// Join without separator
print_r(implode($InputArray));
print_r("<BR>");
// Join with separator
print_r(implode("-",$InputArray));
?>
<html><body>
11.
<?php
$str = "PHP: Welcome to the world of PHP";
print_r(explode(" ",$str));
?>
</body></html>
12.
<html><body>
<?php
$al=array("CO"=>"Computer
Engg","IF"=>"Information Tech","EJ"=>"Electronics
and Telecomm");
$result=array_flip($al);
print_r($result);
?>
</body></html>
13.
<?php
$course = array("CO", "IF", "EJ", "ME", "CE");
echo "Looping using foreach: <br>";
foreach ($course as $val) {
echo $val . "<br>";
}
$total = count($course);
echo "<br>The number of elements are
$total <br>";
echo "Looping using for: <br>";
for ($n = 0; $n < $total; $n++) {
echo $course[$n] . "<br>";
}?>
14.
<HTML>
<HEAD><TITLE>Modifying an array</TITLE>
</HEAD>
<BODY>
<H1>Modifying an array</H1>
<?php
$course[0] = "Computer Engg";
$course[1] = "Information Tech";
$course[2] = "Electronics and Telecomm";
echo "<h2>Before modification</h2>";
echo $course[0] . "<BR>";
echo $course[1] . "<BR>";
echo $course[2] . "<BR>";
echo "<br><h2>After modification</h2>";
$course[2] = "Mechanical Engg";
$course[] = "Civil Engg";
for ($i = 0; $i < count($course); $i++) {
echo $course[$i] . "<BR>";
}
?></BODY></HTML>
15.
<HTML><HEAD><TITLE>Modifying
an Array</TITLE></HEAD>
<BODY>
<H1>Deletion of Array Element</H1><?
php
$course[0] = "Computer Engg";
$course[1] = "Information Tech";
$course[2] = "Electronics and
Telecomm";
echo "<h2>Before Deletion</h2>";
echo $course[0], "<BR>";
echo $course[1], "<BR>";
echo $course[2], "<BR>";
echo "<br><h2>After Deletion</h2>";
$course[2] = "";
// Loop through the array
for ($i = 0; $i < count($course); $i++) {
echo $course[$i], "<BR>";
}
?></BODY></HTML>
16.
<?php
$num = array(40, 61, 2, 22, 13);
echo "Before Sorting:<br>";
$arrlen = count($num);
for ($x = 0; $x < $arrlen; $x++) {
echo $num[$x] . "<br>"; }
sort($num);
echo "After Sorting in Ascending
order:<br>";
for ($x = 0; $x < $arrlen; $x++) {
echo $num[$x] . "<br>";
}
rsort($num);
echo "After Sorting in Descending order:<br>";
for ($x = 0; $x < $arrlen; $x++) { // Reused $arrlen
echo $num[$x] . "<br>";
}?>
17.
<?php
$percent = array("Manisha"=>"80", "Yogita"=>"78",
"Siddhesh"=>"68");
echo "<b>Sorting according to
Value:</b><br>";
asort($percent);
foreach($percent as $x => $x_value)
{
echo "Key=".$x . ", Value=" . $x_value;
echo "<br>";
}
echo
"<br><br><b>Sorting According to Key:</b><br>";
ksort($percent);
foreach($percent as $x => $x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
} ?>
18.
<?php
$course[0] = "Computer Engg";
$coursecourse[1] = "Information Tech";
$course [2] = "Electronics and Telecomm";
echo "<h2> Before Splitting
array:</h2>";
echo $course[0], "<BR>";
echo $course[1], "<BR>";
echo $course[2], "<BR>";
echo "<br><h2>After Splitting:</h2>";
$subarray = array_slice($course, 1, 2);
foreach ($subarray as $value)
{
echo "Course: $value<br>";
}
?>
19.
<?php
$sem3 = array("Object Oriented Programming", "Principle of Database", "Data
Structure");
$sem4 = array("Database Management", "Java Programming", "Software
Engg.", "Computer Network");

$subject = array_merge($sem3, $sem4); //


Corrected variable name and syntax
foreach ($subject as $value) {
echo "Subject: $value<br>";
}
?>

You might also like