Sorting an Array
Ordering data in an increasing or decreasing fashion.
1) Sort: Sorting an index array in ascending order.
<?php
$num=array(2,5,3,4,1);
sort($num);
$length=count($num);
for($i=0;$i<$length;$i++)
{
echo $num[$i];
echo “<br>”;
}
?>
2) rsort: Sorting an index array in descending order.
<?php
$num=array(2,5,3,4,1);
rsort($num);
$length=count($num);
for($i=0;$i<$length;$i++)
{
echo $num[$i];
echo “<br>”;
} ?>
3) asort: Sorting an associative array in ascending order according to value.
<?php
$age=array(“Ram”=>29,”Hari”=>39,”Gopal”=>19);
asort($age);
$length=count($age);
foreach($age as $key=>$value)
{
echo “Key : “.$key.” & Value : “.$value;
echo”<br>”;
} ?>
4) ksort: Sorting an associative array in ascending order according to key.
<?php
$age=array(“Ram”=>29,”Hari”=>39,”Gopal”=>19);
ksort($age);
$length=count($age);
foreach($age as $key=>$value)
{
echo “Key : “.$key.” & Value : “.$value;
echo”<br>”;
}
?>
5)arsort: Sorting an associative array in descending order according their value.
<?php
$employee=array(“Ram”=>1,”Hari”=>2,”Gopal”=>5,”Ramila”=>3,<br ?–> “kundhan”=>4,);
arsort($employee);
$length=count($employee);
foreach($employee as $key=>$value)
{
echo “Key : “.$key.” & Value : “.$value;
echo”<br>”;
}
?>
6) krsort: Sorting an associative array in descending order according their key.
<?php
$age = array(“Peter”=>”35”, “Ben”=>”37”, “Joe”=>”43”);
arsort($age);
foreach($age as $x => $x_value) {
echo “Key=” . $x . “, Value=” . $x_value;
echo “<br>”;
}
?>
Q . Store the percentage of 4 students in an array ,so that % in a linear way & display the results in a tabular form.
<?php
$student=array(“Ram”=>70,”Hari”=>76,”Gopal”=>56,”Ramila”=>67,);
arsort($student);
?>
<table border=”1”>
<tr>
<th>SN.No</th>
<th>Name</th>
<th>Percentage</th>
</tr>
<?php
$i=1;
foreach($student as $key=>$value)
{
echo “<tr>”.
“<td>”.$i.”</td>”.
“<td>”.$key.”</td>”.
“<td>”.$value.”</td>”.
“</tr>”;
$i++;
}
?>
</table>
SN.No | Name | Percentage |
---|---|---|
1 | Hari | 76 |
2 | Ram | 70 |
3 | Ramila | 67 |
4 | Gopal | 56 |
PHP function with parameter
<?php
function addnum($num1,$num2)
{
echo”sum of the given numbers : “.($num1+$num2);
echo”<br>”;
}
function subnum($num1,$num2)
{
echo”Difference of the given numbers : “.($num1-$num2);
echo”<br>”;
}
function product($num1,$num2)
{
echo”product of the given numbers : “.($num1*$num2);
echo”<br>”;
}
function Division($num1,$num2)
{
echo”Division of the given numbers : “.($num1/$num2);
echo”<br>”;
}
function Remender($num1,$num2)
{
echo”Remender of the given numbers : “.($num1%$num2);
echo”<br>”;
}
function operation($num1,$num2,$opr)
{
switch($opr)
{
case “+”:
addnum($num1,$num2);
break;
case “-“:
subnum($num1,$num2);
break;
case “*”:
product($num1,$num2);
break;
case “/”:
Division($num1,$num2);
break;
case “%”:
Remender($num1,$num2);
break;
default:
echo”Invalid operators you Entered”;
}
} operation(2,5,”+”);
operation(7,2,”-“);
operation(29,5,”/”);
operation(60,7,”%”);
operation(50,45,”*”);
?>
output:-
sum of the given numbers : 7
Difference of the given numbers : 5
Division of the given numbers : 5.8
Remender of the given numbers : 4
product of the given numbers : 2250
Pass by value
function addfive(&$num)
{
$num+=5;
}
function addsix(&$num)
{
$num+=6;
}
$orginum=10;
addfive($orginum);
echo “orginal value is :”.$orginum.”<br>”;
addsix($orginum);
echo “orginal value is :”.$orginum;
?>
output:-
orginal value is :10
orginal value is :10
Pass by reference
<?php
function addfive(&$num)
{
$num+=5;
}
function addsix(&$num)
{
$num+=6;
}
$orginum=10;
addfive($orginum);
echo “orginal value is :”.$orginum.”<br>”;
“;
addsix($orginum);
echo “orginal value is :”.$orginum;
?>
WAP to store a information with an array displaying the value of array using function also display the no. of elements using an array
<?php
$age=array(“Ram”=>20,”Hari”=>21,”Gita”=>22);
function display($me)
{
asort($me);
foreach($me as $key=>$value)
{
echo ” key : “.$key.” value : “.$value.”<br>”;
}
$length=count($me);
echo “Total no of elements is :”.$length;
}
display($age);
?>
Funtion returning a value
<?php
function subnum($num,$num2)
{
$difference=$num-$num2;
return $difference;
}
$result=subnum(20,10);
echo “difference of two number :”.$result.”<br>”;
function addnum($num,$num2)
{
$sum=$num+$num2;
return $sum;
}
$result1=addnum(5,10);
echo “sum of the two number :”.$result1.”<br>”;
?>
WAP in php to take input from the user using form and calculate the factorial.
<body>
<form name=”calfactorial” method=”POST” action=” “>
<label for=”name”> Enter Your Number for Calculation of Factorial : </label>
<input type=”number” name=”num”><br>
<button type=”submit” name=”submission”> Submit</button>
</form>
</body>
</html>
<?php
if($_SERVER[‘REQUEST_METHOD’]==”POST”)
{
$number=$_REQUEST[‘num’];
if(empty($number))
{
echo “OHH !!! you not provide number so please input number first !!! “.”<br>”;
}
else {
function fact ($factnum)
{
$facts=1;
if ($factnum==0) {
echo “Factorial of “.$factnum.” is : 1 ” ;
}
else {
for ($i = 1 ; $i <= $factnum ; $i++)
{
$facts=$facts*$i; # code…
}
/* Alternativeldy we can use following code for calculation of factorial.
for ($i=$factnum ; $i<=$factnum ; $i–)
{
$facts=$facts*$i; # code… }*/ print “Factorial of “.$factnum.” is : “.$facts; } } fact($number);
} } ?>
Output:-
Factorial of 5 : 120
Change into UPPER or lower case
<html>
<body>
<form action=”” method=”post”>
<label>Input Any words : <label/>
<input type=”text” name=”word” value=”
<?php
if(isset($_POST[‘WORD’])) {
echo ($_POST[‘WORD’]);
}
?>”>
<button type=”submit” name=”a”>TO UPPER</button>
<button type=”submit” name=”b”>to lower</button>
</form>
?php
if(isset($_POST[‘a’]))
{
$name=$_POST[‘word’];
$abcd=strtoupper($name);
echo”<br>”.$abcd;
}
if(isset($_POST[‘b’])) {
$name=$_POST[‘word’];
$abcd=strtolower($name);
echo”<br>”.$abcd;
}
?>
</body>
</html>
Types of Array
<h3> Numeric Array :-</h3><br>
<?php
$num = array(1,2,3,4,5,6,7,8,9);
echo “Use particular print option : “.”<br>”.$num[4].”<br>”;
echo “OR”.”<br><br>”.”Another way by using printr : ” .”<br>”;
?>
<pre>
<?php
print_r($num);
echo “<br><br>”;
?>
<h3> Associative Array :-</h3><br>
<?php
$salaries = array(‘Ram’ => 20000, ‘Hari’=> 30000);
?>
<table border=”2px”>
<tr>
<th>Name</th>
<th>Web</th>
<th>DBMS</th>
<th>DSA</th>
</tr>
<?php foreach ($marks as $key => $value)
{
echo ‘<tr>’;
echo'<td>’.$key.'</td>’;
foreach <?php
print_r($salaries);
echo “<br><br>”;
?>
<h3> Multidimensational Array :-</h3><br>
<?php
$marks = array(
“Gopal” => array(
‘web’=>20,
‘DBMS’=>40,
‘DSA’=>60
),
“Hari” => array(
‘web’=>24,
‘DBMS’=>43,
‘DSA’=>62
)
);
WAP to calculate the percentage of any student assumed your own subjct and display the division based on the percentage.
?php
$math=70;
$science=80;
$cs=60;
$wt=50;
$dms=90;
$per=($math+$sciece+$cs+$wt+$dms)/5;
if($per>=32 && $per<=40)
{
echo”<br> You Have Got Third Division With Total Marks : “.$per;
}
}
else if($per>40 && $per<=60)
{
echo”<br> You Have Got Second Division With Total Marks : “.$per;
}
else if($per>60 && $per<=80)
{
echo”<br> You Have Got First Division With Total Marks : “.$per;
}
else if($per>80 && $per<=100)
{
echo”<br> You Have Got District Level With Total Marks : “.$per;
}
else
{
echo”Sorry!!! you fail”.$per;
}
?>
Result
You Have Got First Division With Total Marks : 70
Modifying Arrays
1. <?php
$array = array(1,2,3,4,5);
$length=count($array);
for($i=0;$i<$length;$i++)
{ $array[$i] = $array[$i]+10;
echo”value : “.$array[$i];
echo”<br>”;
?>
Output:-
value : 11
value : 12
value : 13
value : 14
value : 15
2.<?php
$array =array(1,2,3,4,5);
$array[2]=7;
foreach($array as $value)
{
echo $value.”<br>”;
}
?>