PHP TASKS
array combine and array merge =>
<!-- TAKE 4 index array, merging the array using array_combine function.
-->
<?php
$a = array("red","green","blue","orange");
$b = array("yellow","brown","silver","purple");
$c = array("gray","white","black","golden");
$d = array("violet","skyblue","pink","silver");
$e = array_combine($b, $a);
$e2 = array_combine($c, $d);
print_r($e);
echo "<br>=====<br>";
print_r($e2);
echo "<br>=====<br>";
print_r (array_merge($e, $e2));
// array_combine
// $a = array("red","green","blue","orange");
// $b = array("yellow","brown","silver","purple");
// $c = array("gray","white","black","golden");
// $d = array("violet","skyblue","pink","silver");
// $no = array(4,3,5,6);
// $e = array_combine($no, $a);
// $res = array_combine($no,$e);
// $res2= $res3 = $res4 = $res;
// echo "LEVEL 1 <br>";
// print_r($res);
// echo "<br>=====<br>";
// $flag = array($res, $res2, $res3, $res4);
// // print_r($flag);
// $res = array_combine($no,$flag);
// echo "LEVEL 2 <br>";
// print_r($res);
// // Level 2 of combine
// echo "<br>=====<br>";
// echo "<br>=====<br>";
// $res2= $res3 = $res4 = $res;
// $flag = array($res, $res2, $res3, $res4);
// // print_r($flag);
// $res = array_combine($no,$flag);
// echo "LEVEL 3 <br>";
// print_r($res);
// // Level 3 of combine
// echo "<br>=====<br>";
// echo "<br>=====<br>";
// $res2= $res3 = $res4 = $res;
// // print_r($res);
// $flag = array($res, $res2, $res3, $res4);
// // print_r($flag);
// $res = array_combine($no,$flag);
// echo "LEVEL 4 <br>";
// print_r($res);
// Level 4 of combine
?>
arrayfunction.php
<?php
$a = array("red","green","blue");
$b = array_pop($a);
print_r($a);
echo "<br>===array_pop==<br>";
print_r($b);
echo "<br>=====<br>";
array_push($a, "yellow","orange");
print_r($a);
echo "<br>==array_push===<br>";
// array_reverse($a);
print_r(array_reverse($a));
// var_dump($a);
echo "<br>=====<br>";
// array_reverse($a);
$b= array_reverse($a);
print_r($b);
echo $b[0];
echo "<br>===array_reverse==<br>";
// array_shift
print_r($a);
array_shift($a);
echo "<br>";
print_r($a);
echo "<br>===array_shift==<br>";
// array_sum :
$aa = array(1,2,3,"l");
print_r(array_sum($aa));
echo "<br>==array_sum===<br>";
// array_combine
$a = array("red","green","blue","orange");
$b = array(4,5,6,7);
$c = array_combine($b, $a);
print_r($c);
echo "<br>===array_combine==<br>";
$a = array(array("red","green","blue","orange"),"green","blue","orange");
$b = array(4,5,6,7);
$c = array_combine($b, $a);
print_r($c);
echo "<br>==array(array==array_combine=<br>";
// count
$a = array("red","green","blue","orange");
$n = count($a);
echo $n;
echo "<br>==count===<br>";
// array_marge
$a = array("red","green","blue","orange");
$b = array(1,2,3);
$c = array(4,5);
$d = array_merge($a, $b, $c);
print_r($d);
echo "<br>==array_merge===<br>";
// Implode
$a = array("red","green","blue","orange");
$b = implode(", ",$a);
echo $b;
echo "<br>==implode===<br>";
// explode
$as = "Today is our first class in array function";
$ba = explode(" ",$as);
print_r($ba);
echo "<br>==explode===<br>";
// sizeof
$a = array("red","green","blue","orange");
$n = sizeof($a);
echo $n;
echo "<br>==sizeof===<br>";
// Associative sort
$a= array("b"=>20, "a"=>10, "c"=>30, 1=>"saif");
foreach ($a as $key => $value) {
// code...
echo "key is ".$key. " and the value is ".$value."<br>";
}
echo "<br>-----foreach (a as key => value)------<br>";
var_dump($a);
echo "<br>------var_dump-----<br>";
asort($a);
foreach ($a as $key => $value) {
// code...
echo "key is ".$key. " and the value is ".$value."<br>";
}
echo "<br>-----asort------<br>";
arsort($a);
foreach ($a as $key => $value) {
// code...
echo "key is ".$key. " and the value is ".$value."<br>";
}
echo "<br>------arsort(a);-----<br>";
ksort($a);
foreach ($a as $key => $value) {
// code...
echo "key is ".$key. " and the value is ".$value."<br>";
}
echo "<br>-----ksort------<br>";
krsort($a);
foreach ($a as $key => $value) {
// code...
echo "key is ".$key. " and the value is ".$value."<br>";
}
echo "<br>-----krsort------<br>";
$a = array(5,4,6,1,2,7,8,9);
print_r($a);
echo "<br>------sort-----<br>";
sort($a);
print_r($a);
echo "<br>-------rsort----<br>";
rsort($a);
print_r($a);
echo "<br>-----------<br>";
?>
array in array =
<?php
$a = array(1,2,3,array(5,6,7),8,9);
print_r($a);
echo "<br>";
print_r($a[3]);
?>
matrix addition and diagonal print =
<?php
$a = array(array(1,2,3),array(4,5,6),array(7,8,9));
echo "Matrix of A is: <br>--------<br>";
for($i=0; $i<3; $i++){
for($j=0; $j<3; $j++){
echo $a[$i][$j]." | ";
}
echo "<br>--------<br>";
}
echo "<br>--------";
$b = array(array(1,2,3),array(4,5,6),array(7,8,9));
echo "<br>Matrix of B is: <br>--------<br>";
for($i=0; $i<3; $i++){
for($j=0; $j<3; $j++){
echo $b[$i][$j]." | ";
}
echo "<br>--------<br>";
}
echo "<br>--------";
$add = array();
$diagonal = 0;
for($i=0; $i<3; $i++){
for($j=0; $j<3; $j++){
$add[$i][$j] = $a[$i][$j] + $b[$i][$j];
if($i == $j)
$diagonal += $add[$i][$i];
}
}
echo "<br>Matrix of Add is: <br>-----------<br>";
for($i=0; $i<3; $i++){
for($j=0; $j<3; $j++){
echo $add[$i][$j]." | ";
}
echo "<br>--------<br>";
}
echo "<br>--------";
echo "<br>-----------<br>";
echo "The diagonal value is: ".$diagonal;
echo "<br>--------";
echo "<br>-----------<br>";
array_string.php
<?php
$a = array("array","string");
$a = "YHIS IS A STRING";
// if(gettype($a)=='array') {
// echo "It is an Array";
// }
// if(gettype($a)=='string'){
// echo "It is an String";
// }
if(is_array($a)){
echo "It is an Array";
}else{
echo "It is an String";
}
?>
fast_last.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title> </title>
</head>
<body>
<form method="POST" action="first_last.php">
<input type="text" name="values" placeholder="Enter any String.." id="v1">
<input type="submit" name="submit" id="Submit">
</form>
</body>
</html>
fast_last.php
<?php
if(isset($_REQUEST['values']))
{
$text = $_REQUEST['values'];
$array = explode(" ",$text);
$firstword = array_shift($array);
$lastword = array_pop($array);
echo "First word is:" .$firstword . " <br>Last word is: ".$lastword;
}
?>
<?php
$a = array(array(1,2,3),array(4,5,6),array(7,8,9));
echo "Matrix of A is: <br>--------<br>";
for($i=0; $i<3; $i++){
for($j=0; $j<3; $j++){
echo $a[$i][$j]." | ";
}
echo "<br>--------<br>";
}
echo "<br>--------";
$b = array(array(1,2,3),array(4,5,6),array(7,8,9));
echo "<br>Matrix of B is: <br>--------<br>";
for($i=0; $i<3; $i++){
for($j=0; $j<3; $j++){
echo $b[$i][$j]." | ";
}
echo "<br>--------<br>";
}
echo "<br>--------";
$add = array();
$diagonal = 0;
for($i=0; $i<3; $i++){
for($j=0; $j<3; $j++){
$add[$i][$j] = $a[$i][$j] + $b[$i][$j];
$diagonal += $add[$i][$i];
}
}
matrix.php
echo "<br>Matrix of Add is: <br>-----------<br>";
for($i=0; $i<3; $i++){
for($j=0; $j<3; $j++){
echo $add[$i][$j]." | ";
}
echo "<br>--------<br>";
}
echo "<br>--------";
echo "<br>-----------<br>";
echo "The diagonal value is: ".$diagonal;
echo "<br>--------";
echo "<br>-----------<br>";
Positive negative.php
<?php
$a = array(1,2,3,4,-1,0,-2,0,0,7);
$pos = array();
$neg = array();
$zer = array();
for ($i= 0; $i < count($a); $i++){
if($a[$i]==0)
array_push($zer,$a[$i]);
else if($a[$i]>0)
array_push($pos,$a[$i]);
else
array_push($neg,$a[$i]);
}
echo "Positive Nos are: ".implode(", ", $pos)." and no of occurances are: ".count($pos)."<br>Negative Nos are: ".implode(", ",$neg)." and no of occurances are: ".count($neg)."<br>Zero Nos are: ".implode(", ",$zer)." and no of occurances are: ".count($zer);
?>
String array.php
<?php
// H>W>Take 2 index array, first merge this array then convert into a string. Reverse of the given string and find the first alphabet of the given string.
$a = array(1,2,3,4,5,6,7,8);
echo "A is ";
print_r($a);
$b = array(19,20,22,23,45,56,12,24);
echo "<br>B is ";
print_r($b);
$c = array_merge($a, $b);
echo "<br>After merging C is ";
print_r($c);
$st = implode(" ",$c);
echo "<br>After String coversion Str is ".$st;
$rst = strrev($st);
echo "<br>After Reversing String Str is ".$rst;
echo "<br>First alphabet is ". substr($rst, 0, 1);
// echo "<br><br>First Word is ". strtok($rst, " ");
// // string to kth index
// echo "<br><br>First Word is ". strstr($rst, " ",true);
?>
String_fun.php
<?php
$str = "hello, how are you";
$str1 = "Hello, today is friday";
echo ucfirst($str);
// uppercase first
echo "<br>";
echo lcfirst($str1);
// lowercase first
echo "<br>";
echo ucwords($str);
// uppercase words
echo "<br>";
echo strrev($str);
// reverse
echo "<br>";
echo str_word_count($str);
// count no of words
echo "<br>";
echo str_replace("today", "it", $str1);
// count no of words
echo "<br><br>strstr<br>";
echo strstr($str,'o');
// Occurance of the string, case-sensitive function
echo "<br><br>stristr<br>";
echo stristr($str,'H');
// Occurance of the string, case-insensitive
echo "<br><br>strlen<br>";
echo strlen($str);
// count the string
echo "<br><br>strpos<br>";
echo strpos($str,"how");
// count the string
echo "<br><br>strrpos<br>";
echo strrpos($str,"hello,");
// count the string reverse postion like mirror
echo "<br><br>strripos<br>";
echo strripos($str,"Hello,");
// count the string reverse postion like mirror case-insensitive
echo "<br><br>strripos<br>";
echo strripos($str,"Hello,");
// count the string reverse postion like mirror case-insensitive
echo "<br><br>md5<br>";
echo md5("Saifuddin");
echo "<br>";
// Hieroglyph
?>
No of vowel consonent etc
<?php
// Take a word "Apple is" Find how many vowels and consonent are present.
$a = 'Apple is';
$v=0;
$c=0;
$vowels = ['a','e','i','o','u'];
$whitespace= [" "];
$w=0;
for($i=0; $i< strlen($a); $i++){
// echo $a[$i];
if (in_array(strtolower($a[$i]), $vowels)) {
echo $a[$i]." is a Vowel<br>";
$v++;
}
else if (in_array(strtolower($a[$i]), $whitespace)){
echo $a[$i]." is a WhiteSpace<br>";
$w++;
}
else{
echo $a[$i]." is a Consonant<br>";
$c++;
}
}
echo "<br> No of Vowels are: ".$v;
echo "<br> No of Consonant are: ".$c;
echo "<br> No of WhiteSpace are: ".$w;
?>
Comments
Post a Comment