You are on page 1of 11

Chapter – 2

Arrays, Functions and Graphics


Marks- 16

Content outline:
2.1. Creating and Manipulating Array
2.2. Extracting data from arrays
2.3. Traversing arrays
2.4. Function and its types
2.5. Operations on String and String functions
2.6. Basic Graphics Concepts

2.1 Creating and Manipulating Array


Q. What is Array?
Ans:
- Arrays in PHP is a type of data structure that allows to store multiple elements of similar
data type under a single variable.
- Arrays used to create a list of elements which can be accessed using their index or key.
- An array is created using an array() function in PHP.
- Types of Array
- 1. Indexed or Numeric Arrays
- 2. Associative Arrays
- 3. Multidimensional Arrays

1. Indexed or Numeric Arrays:


- This is an array with a numeric index where values are stored linearly.
- Numeric arrays use number as access key.
- An access key i.e index is used to access value from an array.
- Syntax :
$array_name = array(value1,value2,……….);
OR
$array_name = array(index=>value1, index=>value2, ……);
- Example:

Notes By-G. R. Jagtap


1
<html>
<body>
<?php
$a[0]=”Apple”;
$a[1]=”Mango”;
$a[2]=”Grapes”;
print_r($a);
/*for($i=0;$i<3;$i++)
echo $a[$i].” “; */
$b=array(10,20,30,40,50);
print_r($b);
?>
</body>
</html>
2. Associative Arrays:
- This type of array is similar to indexed array but index can be numeric and other
type.
- In this type of array, elements can be stored at any order. Instead of linear storage,
elements can be stored at user defined location.
- Syntax:
- $array_name[‘key_name’] = value;
- Example:
<?php
$a = array(“first”=>”Rahul”, “second”=>”Sham”,”third”=>”Riya”);
$b=array(“name”=>”Rahul”,”rno”=>5,”mob”=>983834);
print_r($a);
?>
3. Multidimensional Arrays:
- This array contain other nested array.
- Advantage of multidimensional array is, it allows to create group of array.
- Syntax:
- $array_name = array(
array( elements ……),
array( elements ……),

Notes By-G. R. Jagtap


2
array( elements ……),
……….
);
- Example:
<html>
<body>
<?php
$num_arr = array( array(1,2,3), array(4,5,6), array(7,8,9));
for($a=0;$a<3;$a++)
{
for($b=0;$b<3;$b++)
{
echo $num_arr[$a][$b]. “ ”;
}
echo “</br>”;
}
?>
</body>
</html>

2.2 How to extract data from array?


Q. How to extract data from array?
Ans:
- Data from array can be extracted using extract() function.
- Also list() function can be used to extract data from array.
- Example of extract() function:
<html>
<body>
<?php
$courses[“CE”] = “Civil Engg”;
$course[“CO”] = “Computer Engg”;
$course[“EE”] = “Electrical Engg”;
$course[“EJ”] = “Electronics & Tele. Comm”;
$course[“ME”] = “Mechanical Engg”;

Notes By-G. R. Jagtap


3
echo $course[“EJ”];
extract($course);
echo “1-$CE <br>”;
echo “2-$CO <br>”;
echo “3-$EE <br>”;
echo “4-$EJ <br>”;
echo “5-$ME <br>”;
?>
</body>
</html>

- Example of list() function:


<html>
<body>
<?php
$course[“CE”] = “Civil Engg”;
$course[“CO”] = “Computer Engg”;
$course[“EE”] = “Electrical Engg”;
$course[“EJ”] = “Electronics & Tele. Comm”;
$course[“ME”] = “Mechanical Engg”;
list($one, $two, $three) = $course;
echo $one.”<br>”;
echo $two.”<br>”;
echo $three.”<br>”;
?>
</body>
</html>

Q. What is the use of compact() function?


Ans:
- This function is opposite of extract() function.
- It returns array with all the variables added to it.
- The compact() function creates associative array whose key value are the variable
name and whose values are the variable values.

Notes By-G. R. Jagtap


4
- Example:
<html>
<body>
<?php
$var1 = “Civil Engg”;
$var2 = “Computer Engg”;
$var3 = “Electrical Engg”;
$var4 = “E & TC Engg”;
$var5 = “Mechanical Engg”;
$a = compact(“var1”,”var2”,”var3”,”var4”,”var5”);
print_r($a);
?>
</body>
</html>

Q. Describe use of implode() and explode() function.


Ans:
- The implode() function implodes an array into a string and explode() function explodes a
string into an array.
- implode()
- It is a built in function of PHP.
- This function is used to join the elements of an array.
- The implode() function returns a string form by joining the elements of array.
- Syntax:
string implode(separator, array);
- The implode() function accepts two parameters.
- Separator: this is an optional parameter. It is of string type. Elements of array will
be join to form a string and will be separated by the separator. If not provided, the
default value is “” (i.e empty string). This parameter is optional.
- Array: this parameter specifies the array whose value to be joined to form a string.
This parameter is compulsory.
- Example:
<html>
<body>

Notes By-G. R. Jagtap


5
<?php
$a = array(“Civil”, ”Computer”, ”Electrical”, ”E&TC”, ”Mechanical”);
$b=implode(“,”, $a);
echo “Output of implode is –“. $b;
?>
</body>
</html>

- explode()
- This is built in function of PHP.
- The explode function breaks a string into an array.
- This function splits a string based on delimeter.
- Syntax:
- array explode(separator, original_string, no_of_elements)
- The explode function accepts 3 parameters.
- First two parameters i.e separator and original_string are compulsory and no_of_elements
is optional.
- Separator: this character specifies at which point to split the string.
- original_string: this is input string which is to be split in array.
- no_of_elements: it is used to specify the number of elements of the array. Value of this
parameter can be positive, negative or zero.
- Positive value(N) – if the number of elements after separating with respect to the separator
emerges to be greater than this value the first N-1 elements remain same and the last
element is the whole remaining string.
- Negative value (N) - if negative value is passes as parameter then first N elements of the
array will trimmed out and remaining part of the string shall be returned as a array.
- Zero – if this parameter is zero then the array returned will have only one element i.e whole
string.
- Example:
- <html>
- <body>
- <?php
$str = “Hello, welcome to the world of fun”;
$a=explode(“ “, $str);

Notes By-G. R. Jagtap


6
echo “Output is >br>”;
print_r($a);
- ?>
- </body>
- </html>

Q. Explain Functions of Array.


Ans:

1) array_flip():
- This function is exchanges key with their associated values.
- This is built in function of PHP.
- Syntax : array_flip(array)
- Example:
<?php
$a = array(“first”=>”Rahul”, “second”=>”Sham”,”third”=>”Riya”);
echo “Array before flip”;
print_r($a);
$res = array_flip($a);
echo “Array after flip”;
print_r($res);
?>
2) Traversing array
3) Modifying data in arrays
4) Deleting array elements
5) Array values
6) Sorting arrays
7) Splitting and merging arrays

String functions:
<html>

Notes By-G. R. Jagtap


7
<head>
<title> Use of String Functions</title>
</head>
<body>
<?php
//chop()- Remove characters from the right end of a string
$str = "Hello Students";
//echo $str . "<br>";
echo chop($str,"ents");
echo "<br>";

//echo()
echo "Welcome to world of fun";
print "<br>";

//explode() - Break a string into an array.


$str = "Hello all. Now start learning with fune. Have a nice day.";
print_r (explode(" ",$str));
print "<br>";

//implode()- Join array elements and generates a string.


$arr = array('Hello','Students','Welcome','to','GGSP');
echo implode(" ",$arr);
print "<br>";

//join()- Join array elements and generates a string.


$arr = array('Hello','Hi!','How','are','you?');
echo join(" ",$arr);
print "<br>";

//lcfirst()- Convert the first character of "Hello" to lowercase


$str=”Hello”;
echo lcfirst($str);//hello
echo lcfirst("Hello Students!");

Notes By-G. R. Jagtap


8
print "<br>";

//
$str = "Hello Hello Students.........";
echo $str . "<br>";
echo ltrim($str,"Hello");
print "<br>";

//
print "Hello world!";
print "<br>";

//
$number = 99;
$str = "Sakshi";
printf("SSC percentage of %s - %u",$str,$number);
print "<br>";

//
$str = "Hello Students";
echo $str . "<br>";
echo rtrim($str,"Students");
print "<br>";

//
echo str_repeat("Hello",6);
print "<br>";

//what to be replaced, by what, in what(case sensitive)


echo str_replace("world!","Peter","Hello World!");
print "<br>";

//space is considered.
print_r(str_split("Hello Welcome"));

Notes By-G. R. Jagtap


9
print "<br>";

//
echo str_word_count("Hello Students. Welcome to the world of fun.");
print "<br>";

//
echo strcasecmp("Hello World!","HELLO WORLD!");
print "<br>";

//
echo "--";
echo strchr("Hello world!","world");
print "<br>";

//
echo "--";
echo strcmp("Hello world!","Hello world!");
print "<br>";

//
echo strlen("Hello");
print "<br>";

//
echo stripos("I am Final year student of computer engg","Final");
print "<br>";

//
echo stristr("Hello world!","WORLD");
print "<br>";

//
echo strpos("I love php, I love php too!","php");

Notes By-G. R. Jagtap


10
print "<br>";

//
echo strrev("Welcome to GGSP");
print "<br>";

//
echo strrpos("I am student of computer engg of GGSP ","of");
print "<br>";

//
$str = "Hello Students!";
echo $str . "<br>";
echo trim($str,"Hel");
print "<br>";

//
echo ucfirst("hello world!");
print "<br>";

//
echo ucwords("hello world");
print "<br>";

//
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15,"<br>\n");

?>
</body>
</html>

Notes By-G. R. Jagtap


11

You might also like