KEMBAR78
Assignment 33 | PDF | Php | Data Management
0% found this document useful (0 votes)
47 views10 pages

Assignment 33

The document contains PHP programming assignments focused on arrays and strings, divided into three sets. Set A covers operations on indexed and associative arrays, including union, traversal, and element manipulation. Set B and C focus on string operations such as comparison, case conversion, and string reversal.

Uploaded by

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

Assignment 33

The document contains PHP programming assignments focused on arrays and strings, divided into three sets. Set A covers operations on indexed and associative arrays, including union, traversal, and element manipulation. Set B and C focus on string operations such as comparison, case conversion, and string reversal.

Uploaded by

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

Assignment 3

Arrays and Strings


SET A:
1. Write PHP program to perform the following operations on Indexed Array:
a) Union of two arrays
b) Traverse the array elements in random order

Solution:

a) Union of two arrays


<?php
function array_union($x, $y)
{
$aunion= array_merge(
array_intersect($x, $y),
array_diff($x, $y),
array_diff($y, $x)
);
return $aunion;
}
$a = array(1, 2, 3, 4);
$b = array(2, 3, 4, 5, 6);
print_r(array_union($a, $b));
?>

b) Traverse the array elements in random order

2. Write a PHP program to perform the following operations on an associative array:


a) Display the elements of an array along with the keys.
b) Display the size of an array
c) Delete an element from an array from the given index.
d) Reverse the order of each element’s key-value pair
e) Traverse the elements in an array in random order.

Solution:

HTML File-
<html>
<form action='ass2a1.php' method='post'>
<pre>
<h3>OPERATIONS</h3>
1<input type='radio' name='a' value='1'>Display.
2<input type='radio' name='a' value='2'>size.

3<input type='radio' name='a' value='3'>delete.

4<input type='radio' name='a' value='4'>reverse.

5<input type='radio' name='a' value='5'>traverse.

<input type='submit' value='ok'>


<input type='reset' value='cancel'>

</pre>
</form>
</body>
</html>

PHP Function-
NOTE: PHP function is saved as "ass2a1.php"
<?php
$array=array('zero'=>0,'one'=>1,'two'=>2,'three'=>3,'four'=>4,'five'=>5);
$ch=$_POST['a'];
switch($ch)
{
case 1:foreach($array as $key=>$value)
{
echo"key:$key
val:$value.
";
}
break;
case 2:echo sizeof($array);
break;
case 3 :
$len=sizeof($array);
array_splice($array,$len);
print_r( $array);
break;
case 4 :
print_r(array_flip($array));
break;
case 5 :
shuffle($array);
print_r($array);
break;
}
?>

3. Write a PHP Script for the following:


a) Declare and Display a multidimensional Array.
b) Search and display a specific element from a Multidimensional array.
Solution:

<?php
$num=array(array(1,2),array(3,4));
print_r($num);
echo "
";
echo "Display Specific element
";
functiondisplayelement($r,$c)
{
global $num;
echo $num[$r][$c];
}
displayelement(1,1);
echo "
";
echo "
";
echo "Delete Given element form array
";
functiondeleteelement($e)
{
global $num;
for($i=0; $i {
for($j=0;$j {
if($e == $num[$i][$j])
unset($num[$i][$j]);
}
}
}
deleteelement(2);
print_r($num);
echo "
";
echo "
";
echo "Search element
";
$nm=array("a","b","c","d");
if(in_array("a",$nm))
echo "element Found
";
else
echo "Element Not found
";

?>

SET B:
1. Write a PHP script to perform the following operations on string :
i) Compare string 2 with string3.
ii) Convert all the strings to Uppercase
iii) Convert all the strings to Lowercase

<?php

// Declare string
$str = "Geeks^for+Geeks";
print ("Original String \n");

// Split string in characters


$chars = str_split($str);
print ($str. "\n");
print ("Uppercase String \n");

// Looping over characters


foreach ($chars as $ch){

// Check if character is
// small case alphabet
if(ctype_lower($ch)){

// Convert to upper case


echo chr(ord($ch)-32);
}
else{
// Else print character
// unmodified
echo($ch);
}
}
?>

\2. Write a PHP script to perform the following operations on string :


i) Convert each word of a string to Lowercase and Uppercase.
ii) Find the first and last occurrence of string2 in string1.

Solution:

<?php
// Declare string
$str = "Geeks^for+Geeks";
print ("Original String \n");

// Split string in characters


$chars = str_split($str);
print ($str. "\n");
print ("Uppercase String \n");

// Looping over characters


foreach ($chars as $ch){

// Check if character is
// small case alphabet
if(ctype_lower($ch)){

// Convert to upper case


echo chr(ord($ch)-32);
}
else{

// Else print character


// unmodified
echo($ch);
}
}
?>

3. Write a menu-driven program in PHP to perform the following operations on


associative
arrays:
i) Sort the array by values (changing the keys) in ascending, descending order.
ii) Also, sort the array by values without changing the keys.
iii) Find the intersection of two arrays.
iv) Find the union of two arrays.

Solution:

<html>
<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Select options:

<input type="radio" value="Sort the keys" name="d1">Sorting of Keys</input>

<input type="radio" value="Sort the values" name="d1">Sorting of values</input>

<input type="radio" value="Odd elements" name="d1">Filter the odd


elements</input>

<input type="radio" value="Multisort" name="d1">Sort the different arrays at a


glance using single function</input>

<input type="radio" value="Merge" name="d1">Merge the Arrays</input>

<input type="radio" value="Intersection" name="d1">Intersection of two


Arrays</input>

<input type="radio" value="Union" name="d1">Union of two Arrays</input>


<input type="radio" value="Difference" name="d1">Difference of two
Arrays</input>

<input type="submit">

</form>
<?php
echo "
Associative ARRAY.1......";
$a = array("one"=>"1","two"=>"2","three"=>"3");
echo "
Associative ARRAY.2......
";
$a1 = array("four"=>"4","two"=>"2","six"=>"6");
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$op=$_POST['d1'];

if($op == 'Sort the keys')


{
echo"Ascending order
";
sort($a);
foreach($a as $k => $v)
{
echo "Key $k Value $v
";
}
echo"Descending order
";
rsort($a);
foreach($a as $k => $v)
{
echo "Key $k Value $v
";
}
}
else if($op == 'Sort the values')
{
echo"Ascending order
";
asort($a);
foreach($a as $k => $v)
{
echo "Key $k Value $v
";
}
echo"Descending order
";
arsort($a);
foreach($a as $k => $v)
{
echo "Key $k Value $v
";
}
}
else if($op == 'Odd elements')
{
echo "Odd elements";
function test_odd($var)
{
return ($var &1);
}
print_r(array_filter($a,"test_odd"));
}
else if($op =='Multisort')
{
$b1=array_multisort($a,$a1);
print_r($b1);
}
else if($op =='Merge')
{
print_r(array_merge($a,$a1));
}
else if($op =='Intersection')
{
$r = array_intersect($a1,$a);
print_r($r);
}
else if($op =='Union')
{
$b=array_unique(array_merge($a,$a1));
print_r($b);
}
else if($op =='Difference')
{
$b1=array_diff($a,$a1);
print_r($b1);
}
}
?>
</body>
</html>

SET C:
1. Write a PHP script to perform the following operations on string :
i) Replace the string2 by string3 in string1.
ii) Reverse and display the string.

Solution:

<?php

// PHP function to reverse a string using


// recursion and substr()
function Reverse($str){

// strlen() used to calculate the


// length of the string
$len = strlen($str);

// Base case for recursion


if($len == 1){
return $str;
}
else{
$len--;

// extract first character and concatenate


// at end of string returned from recursive
// call on remaining string
return Reverse(substr($str,1, $len))
. substr($str, 0, 1);
}
}

// Driver Code
$str = "GeeksforGeeks";
print_r(Reverse($str));

?>

You might also like