Creating & Manipulating Array, Types of Arrays
Definition
Arrays: It is a collection of items (values or objects) that are arranged sequentially and it can be accessed with the help of index value.
Creating an Array:
Syntax:
$variablename= array(<list of elements>); // Using array function it can be created.
Count ($variablename); // length of an array means number of slots in array.
Manipulating an Array:
It means changing values of arrays. It can be done with the help of an index.
Example: In above diagram values can be given in the following way:
$arr[0] = “A”;
$arr[1] = “M”;
$arr[2] = “N”;
$arr[3] = “Q”;
$arr[4] = “F”;
// changing value at index 3 to “T” means simply assign or override it
$arr[3]=”T”;
Now index 3’s value is changed to “T”.
Types of Array with traversing example:
1. Indexed Array: Index with Numeric value.
2. Associative Array: Index with String or named values.
3. Multidimensional Arrays: Arrays inside array.
1. Indexed Array
It starts with zero by default and increase by 1 automatically.
Example:
<?php
$arr = array("A", "M", "N", "Q", "F");
echo "Values of arr array is " . $arr[0] . ", " . $arr[1] . ", " . $arr[2] . ", " . $arr[3] . ", " . $arr[4]."\n";
echo "Length of arr is ";
echo count($arr);
?>
Output
Values of arr array is A, M, N, Q, F
Length of arr is 5
Loop Example
<?php
$arr = array("A", "M", "N", "Q", "F");
echo "Values of arr array is " . $arr[0] . ", " . $arr[1] . ", " . $arr[2] . ", " . $arr[3] . ", " . $arr[4]."\n";
echo "Length of arr is ";
echo count($arr)."\n";
for($i = 0; $i < count($arr); $i++)
{
echo $arr[$i];
echo "\n"; //this is used to break line and start from newline
}
?>
Output
Values of arr array is A ,M , N , Q , F
Length of arr is 5
A
M
N
Q
F
2. Associative Arrays : Accessed by Name
Example:
<?php
$arr['Ajay'] = "23";
$arr['Abhay'] = "24";
$arr['Jay'] = "27";
//or other way $arr = array("Ajay"=>"23", "Abhay"=>"24", "Jay"=>"27");
echo $arr['Ajay'].",". $arr['Abhay'].",". $arr['Jay']."\n"; /* remember dot .
is used for concatenation or appending string.*/
foreach($arr as $a => $values) {
echo "Name is ".$a.", Age is ".$values;
echo "\n";
}
?>Output:
23,24,27
Name is Ajay, Age is 23
Name is Abhay, Age is 24
Name is Jay, Age is 27
3. Multidimensional Arrays
It is a Matrix like structure with rows and columns. This can be implemented by column array inside row array.
Example:
<?php
$matrix = array
(
array("00","01","02"),
array("10","11","12"),
array("20","21","22")
);
echo $matrix[0][0]." ".$matrix[0][1]." ".$matrix[0][2]."\n";
echo $matrix[1][0]." ".$matrix[1][1]." ".$matrix[1][2]."\n";
echo $matrix[2][0]." ".$matrix[2][1]." ".$matrix[2][2]."\n";
?>
Output:
00 01 02
10 11 12
20 21 22
Extracting data from arrays, implode, explode, and array flip
Implode and Explode
Implode()
|
Explode()
|
It is used to get strings from all elements of array
|
It is used to put strings of all elements into an array.
|
Syntax:
implode(seperator,array-variable);
|
Syntax:
explode(seperator,array-variable,limit);
here limit is optional and it is a decides number of indexes.
-> Greater than 0 limit say 2: Than 2 key or 2 indexes with values.
-> Less than 0 limit: than except last element all elements with own index.
-> O Limit– all with one single index
|
Example:
<?php
$a = array('Hi','I','am','here'); echo implode(" ",$arr); ?>
Output:
Hi I am here
If you use * as separator then
echo implode("*",$arr);
gives Output:
Hi*I*am*here
|
Example:
<?php
$str = 'A,B,C,D'; // zero limit print_r(explode(',',$str,0)); // 2 limit print_r(explode(',',$str,2)); // -1 limit print_r(explode(',',$str,-1)); ?>
Output:
Array ( [0] => A,B,C,D ) //0 limit 1 index [0]
Array ( [0] => A [1] => B,C,D ) //2 limit 2 index[0],[1]
Array ( [0] => A [1] => B [2] => C )//-1 limit each element have index ,[0],[1],[2] except last one
|
It is for joining into string
|
It is for breaking into array
|
Array Flip: It is a function for manipulating associative array by exchanging key and value.
Syntax: array_flip(array-variable);
Example:
<?php
$a=array("1"=>"mon","2"=>"tue","3"=>"wed"
,"4"=>"thu","5"=>"fri","6"=>"sat","7"=>"sun");
$result=array_flip($a);
print_r($result);
?>
Output:
Array ( [mon] => 1 [tue] =>
2 [wed] => 3 [thu] => 4 [fri] => 5 [sat] => 6 [sun] => 7)
Functions and its Type:
Note: Concept of functions and its terms are written in unit 3 Notes please refer it.
Variable Function
|
Anonymous Function
|
Assigned name based function.
|
No Name based function
|
Used to implement callbacks
|
Used as value of callback parameter
|
It can be used to store functions list inside arrays
|
Can be used to store functions in the form of arrays
|
It does not need Closure Class
|
It is implemented using the Closure Class
|
Example:
<?php
function varfunc() {
echo "In varfunc()";
}
$func = 'varfunc';
$func(); // This calls varfunc() ?>
Output;
In varfunc()
|
Example:
<?php
$noName = function($name)
{
printf("Print: ". $name);
};
$noName('It is anonymous'); ?>
Output:
Print: It is anonymous
|
Operation on Strings: Note: please Refer Unit 1 Notes, all string function with example are demonstrated.
Basic Graphics Concepts:
Definition of Graphics: It is a visual representation of object or design in the form of image.
Pixel: It is a smallest dot available in the screen which illuminate color.
Image: It is a collection of pixels, mainly in rectangular form.
Palette: An array of colors which consist of mainly three values of red green and blue with values ranges from 0 to 255.
Antialiasing: It is technique to smooth the edges of image by minimizing stair like appearance by giving same color to neighbor pixels.
Creating Image with text: To Create an Image following functions are used:
1. ImageCreate(width,height) : decide the size of Image and return image.
2. ImageColorAllocate(image, red,blue,green): set the color shade in Color variable.
3. ImageString()// used to display text inside image.
4. ImageFormat for example ImageJPEG($image) will return jpeg image format , ImagePNG($image) will return PNG format.
Example:
<?php
header("Content-Type: image/png");
$image = @imagecreate(100, 30)
or die("Error in initialization of stream");
$backgroundcolor = imagecolorallocate($image, 0, 0, 0);
$color_of_text = imagecolorallocate($image, 120, 15, 80);
imagestring($image, 1, 5, 5, "Text Demo with Image", $color_of_text);
//text inside image
imagejpeg($image); // Image format
imagedestroy($image); //free memory
?>
Output
Text Demo with Image
Scaling Image: Scaling means resizing of image with new width and height.
There is function called imagescale () with following parameters that can be used to resize Image as per user need:
1. $Source_image: location where original image is present.
2. $new_width: modified image’s width value.
3. $new_height: modified image’s height value.
4. mode: Any of the following:
i. IMG_NEAREST_NEIGHBOUR
ii. IMG_BILINEAR_FIXED
iii. IMG_BICUBIC
iv. IMG_BICUBIC_FIXED
Example:
<?php
header("Content-Type: image/png");
$image = @imagecreate(100, 30)
or die("Error in initialization of stream");
$backgroundcolor = imagecolorallocate($image, 0, 0, 0);
$color_of_text = imagecolorallocate($image, 120, 15, 80);
$newScale= imagescale($image, 200, 30);
imagejpeg($image); // Image format
imagedestroy($image); //free memory
?>
Output
___________________________________
Creation of PDF Document: To create pdf in php there are many libraries available, some are:
1. PDFlib: Features:
i. PDF input is accepted
ii. Word detection
iii. Improved Color Controls.
iv. Reduced use of Memory for embedded system etc
2. FPDF: Features:
i. options to measure margins, page format etc
ii. Auto page break
iii. Colors
iv. Links
3. mPDF: Features:
i.It is mainly html to pdf converter.
ii. Support almost all html features into pdf
iii. Good for report generation from processed web page.
iv. Its base is FPDF.
Example:
<?php
//need library mpdf in your project folder before use.
require_once 'mpdf/autoload.php';
$newpdf = new mPDF();
//PDF Contents in HTML Form
$mpdf->WriteHTML('<h1>From HTML</h1><br><p>My PDF</p>');
// Output a PDF file directly to the browser
$mpdf->Output();
?>
No comments:
Post a Comment