SlideShare a Scribd company logo
WORKING WITH ARRAYS
ARRYAS
• Array is a data structure, which provides the
facility to store a collection of data of same
type under single variable name.
• An array is a collection of data values,
organized as an ordered collection of key-
value pairs.
INDEXED VERSUS ASSOCIATIVE ARRAYS
• There are two kinds of arrays in PHP:
• indexed and associative.
• The keys of an indexed array are integers,
beginning at 0.
• Indexed arrays are used when you identify things
by their position.
• Associative arrays have strings as keys and behave
more like two-column tables.
• The first column is the key, which is used to access
the value.
INDEXED VERSUS ASSOCIATIVE ARRAYS
• In both cases, the keys are unique--that is, you
can't have two elements with the same key,
regardless of whether the key is a string or an
integer.
• PHP arrays have an internal order to their
elements that is independent of the keys and
values, and there are functions that you can use to
traverse the arrays based on this internal order.
• The order is normally that in which values were
inserted into the array.
IDENTIFYING ELEMENTS OF AN ARRAY
• You can access specific values from an array using the array
variable's name,
• followed by the element's key (sometimes called the index)
within square brackets:
• $age['Fred']
• $shows[2]
• The key can be either a string or an integer.
• String values that are equivalent to integer numbers (without
leading zeros) are treated as integers.
• Thus, $array[3] and $array['3'] reference the same element,
but $array['03'] references a different element.
IDENTIFYING ELEMENTS OF AN ARRAY
• You don't have to quote single-word strings.
• For instance,
• $age['Fred'] is the same as $age[Fred].
• However, it's considered good PHP style to
always use quotes, because quoteless keys
are indistinguishable from constants.
STORING DATA IN ARRAYS
• Storing a value in an array will create the array if it didn't
already exist.
• For example:
• echo $addresses[0]; // prints nothing
• echo $addresses; // prints nothing
• $addresses[0] = 'spam@cyberpromo.net';
• echo $addresses; // prints "Array"
• $addresses[0] = 'spam@cyberpromo.net';
• $addresses[1] = 'abuse@example.com';
• $addresses[2] = 'root@example.com';
STORING DATA IN ARRAYS
• $price[ 'Gasket‘ ] = 15.29;
• $price[ 'Wheel‘ ] = 75.25;
• $price[ 'Tire‘ ] = 50.00;
• An easier way to initialize an array is to use the
array( ) construct, which builds an array from its
arguments:
• $addresses = array( 'spam@cyberpromo.net',
'abuse@example.com', 'root@example.com‘ );
STORING DATA IN ARRAYS
• To create an associative array with array( ),
• use the => symbol to separate indexes from values:
• $price = array( 'Gasket' => 15.29,
• 'Wheel' => 75.25,
• 'Tire' => 50.00);
• $price = array(
'Gasket'=>15.29,'Wheel'=>75.25,'Tire'=>50.00);
• To construct an empty array, pass no arguments to
array( ):
• $addresses = array( );
STORING DATA IN ARRAYS
• You can specify an initial key with => and then a list of values.
• The values are inserted into the array starting with that key,
with subsequent values having sequential keys:
• $days = array(1 => 'Monday', 'Tuesday', 'Wednesday',
• 'Thursday', 'Friday', 'Saturday', 'Sunday');
• // 2 is Tuesday, 3 is Wednesday, etc.
• If the initial index is a non-numeric string, subsequent indexes
are integers beginning at 0.
• $whoops = array('Friday' => 'Black', 'Brown', 'Green');
• // same as
• $whoops = array('Friday' => 'Black', 0 => 'Brown', 1 =>
'Green');
ADDING VALUES TO THE END OF AN ARRAY
• To insert more values into the end of an existing indexed
array, use the [] syntax:
• $family = array('Fred', 'Wilma');
• $family[] = 'Pebbles'; // $family[2] is 'Pebbles'
• This construct assumes the array's indexes are numbers and
assigns elements into the next available numeric index,
starting from 0.
• Attempting to append to an associative array is almost always
a programmer mistake, but PHP will give the new elements
numeric indexes without issuing a warning:
• $person = array('name' => 'Fred');
• $person[] = 'Wilma'; // $person[0] is now 'Wilma'
VIEWING ARRAYS
• You can see the structure and values of any array by
using one of two functions — var_dump or print_r.
• The print_r() statement, however, gives somewhat
less information.
• To display the $customers array, use the following
functions: print_r($customers);
• To get more information, use the following
functions: var_dump($customers);
MODIFYING ARRAY & STORING ONE ARRAY IN ANOTHER
• Arrays can be changed at any time in the script, just as
variables can.
• The individual values can be changed, elements can be added
or removed, and elements can be rearranged.
• $customers[2] = “John”;
• $customers[4] = “Hidaya”;
• $customers[] = “Dell”;
• You can also copy an entire existing array into a new array
with this statement:
• $customerCopy = $customers;
REMOVING VALUES FROM ARRAYS
• Sometimes you need to completely remove a value from an
array.
• $colors = array ( “red”, “green”, “blue”, “pink”, “yellow”
);
• $colors[ 3 ] = “”;
• Although this statement sets $colors[3] to blank, it does not
remove it from the array. You still have an array with five
values, one of the values being an empty string.
• To totally remove the item from the array, you need to unset
it.
• unset($colors[3]);
REMOVING VALUES FROM ARRAYS
• Removing all the values doesn’t remove the
array itself
• To remove the array itself, you can use the
following statement
• unset($colors);
USING ARRAYS IN STATEMENTS
• Arrays can be used in statements in the same
way that variables are used in.
• You can retrieve any individual value in an
array by accessing it directly, as in the
following example:
• $Sindhcapital = $capitals[‘Sindh’];
• echo $Sindhcapital;
• You can echo an array value like this:
• echo $capitals[‘Sindh’];
GETTING THE SIZE OF AN ARRAY
• The count( ) and sizeof( ) functions are identical in use and
effect.
• They return the number of elements in the array.
• Here's an example:
• $family = array('Fred', 'Wilma', 'Pebbles');
• $size = count($family); // $size is 3
• These functions do not consult any numeric indexes that
might be present:
• $confusion = array( 10 => 'ten', 11 => 'eleven', 12 => 'twelve');
• $size = count($confusion); // $size is 3
WALKING THROUGH AN ARRAY
• Walking through each and every element in an array, in order,
is called iteration.
• It is also sometimes called traversing.
• Two ways to walk through an array:
• Traversing an array manually:
– Uses a pointer to move from one array value to another.
• Using foreach:
– Automatically walks through the array, from beginning to
end, one value at a time.
USING FOREACH TO WALK THROUGH AN ARRAY
• You can use foreach to walk through an array one value at a
time and execute a block of statements by using each value in
the array.
• The general format is as follows:
foreach ( $arrayname as $keyname => $valuename )
{
block of statements;
}
RANGE()
• You can also create an array with a range of values by using
the following function.
$years = range(2001, 2010,[step]);
• Here step is a positive number which has default value 1. Step
is used for number of increment.
LIST()
• You can retrieve several values at once from an array with the
list function.
• The list function copies values from an array into variables.
•
$colors=array(“red”,”green”);
list($red,$green)=$colors;
ARRAY_SLICE()
• You can split an array by creating a new array that contains a
subset of an existing array.
• Works same as substr works for string.
• You can do this by using following function:
$subArray = array_slice($arrayname,start,length);
ARRAY_MERGE()
• Conversely, you can merge two or more arrays together by
using the following function:
$bigArray = array_merge($array1,$array2,...);
ARRAY_SUM()
• To add all the values in an array, use the following function:
• $sum = array_sum($array);
• Of course, you are only going to add elements in an array of
numbers.
• PHP converts strings to 0 if you try to add them.
ARRAY_UNIQUE()
• Removes duplicate items from an array:
$names2 = array_unique($names);
ARRAY_FLIP()
• To swap the keys and values, use the following function:
$arrayFlipped = array_flip($testarray);
extract()
• Extracts array, forms variables with the names of keys or
indexes of array.
• extract($array);
ASSIGMENTS
1. Make a linear Search using array.
2. Take an unsorted array and sort it using bubble sort
technique.
3. Make an array which should contain intersected values of
two given arrays.

More Related Content

What's hot

Windowforms controls c#
Windowforms controls c#Windowforms controls c#
Windowforms controls c#
prabhu rajendran
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
Lemia Algmri
 
Sessions in php
Sessions in php Sessions in php
Sessions in php
Mudasir Syed
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
GirdharRatne
 
Lossless decomposition
Lossless decompositionLossless decomposition
Lossless decomposition
MANASYJAYASURYA
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
Aakash deep Singhal
 
URL
URLURL
URL
naly01
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
Ashim Lamichhane
 
trigger dbms
trigger dbmstrigger dbms
trigger dbms
kuldeep100
 
HTML: Tables and Forms
HTML: Tables and FormsHTML: Tables and Forms
HTML: Tables and Forms
BG Java EE Course
 
View of data DBMS
View of data DBMSView of data DBMS
View of data DBMS
Rahul Narang
 
Chapter 07 php forms handling
Chapter 07   php forms handlingChapter 07   php forms handling
Chapter 07 php forms handling
Dhani Ahmad
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
Taha Malampatti
 
Query optimization
Query optimizationQuery optimization
Query optimization
Pooja Dixit
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arrays
Hassan Dar
 
b+ tree
b+ treeb+ tree
b+ tree
bitistu
 
Database Triggers
Database TriggersDatabase Triggers
Database Triggers
Aliya Saldanha
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
V.V.Vanniaperumal College for Women
 
html-table
html-tablehtml-table
html-table
Dhirendra Chauhan
 
I/O System
I/O SystemI/O System
I/O System
Nagarajan
 

What's hot (20)

Windowforms controls c#
Windowforms controls c#Windowforms controls c#
Windowforms controls c#
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Sessions in php
Sessions in php Sessions in php
Sessions in php
 
Relational algebra ppt
Relational algebra pptRelational algebra ppt
Relational algebra ppt
 
Lossless decomposition
Lossless decompositionLossless decomposition
Lossless decomposition
 
Lecture 1 data structures and algorithms
Lecture 1 data structures and algorithmsLecture 1 data structures and algorithms
Lecture 1 data structures and algorithms
 
URL
URLURL
URL
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
trigger dbms
trigger dbmstrigger dbms
trigger dbms
 
HTML: Tables and Forms
HTML: Tables and FormsHTML: Tables and Forms
HTML: Tables and Forms
 
View of data DBMS
View of data DBMSView of data DBMS
View of data DBMS
 
Chapter 07 php forms handling
Chapter 07   php forms handlingChapter 07   php forms handling
Chapter 07 php forms handling
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Query optimization
Query optimizationQuery optimization
Query optimization
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arrays
 
b+ tree
b+ treeb+ tree
b+ tree
 
Database Triggers
Database TriggersDatabase Triggers
Database Triggers
 
MYSQL - PHP Database Connectivity
MYSQL - PHP Database ConnectivityMYSQL - PHP Database Connectivity
MYSQL - PHP Database Connectivity
 
html-table
html-tablehtml-table
html-table
 
I/O System
I/O SystemI/O System
I/O System
 

Viewers also liked

Php array
Php arrayPhp array
Php array
Core Lee
 
Php array
Php arrayPhp array
Php array
argusacademy
 
PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arrays
Kumar
 
Php array
Php arrayPhp array
Php array
Nikul Shah
 
Arrays in PHP
Arrays in PHPArrays in PHP
Synapseindia reviews on array php
Synapseindia reviews on array phpSynapseindia reviews on array php
Synapseindia reviews on array php
saritasingh19866
 
My self learn -Php
My self learn -PhpMy self learn -Php
My self learn -Php
laavanyaD2009
 
03 Php Array String Functions
03 Php Array String Functions03 Php Array String Functions
03 Php Array String Functions
Geshan Manandhar
 
Array in php
Array in phpArray in php
Array in php
ilakkiya
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros Developer
Nyros Technologies
 
Web app development_php_06
Web app development_php_06Web app development_php_06
Web app development_php_06
Hassen Poreya
 
PHP: Arrays
PHP: ArraysPHP: Arrays
PHP: Arrays
Mario Raul PEREZ
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
Vineet Kumar Saini
 
PHP Arrays
PHP ArraysPHP Arrays
PHP Arrays
Mahesh Gattani
 
Php ppt
Php pptPhp ppt
jQuery For Beginners - jQuery Conference 2009
jQuery For Beginners - jQuery Conference 2009jQuery For Beginners - jQuery Conference 2009
jQuery For Beginners - jQuery Conference 2009
Ralph Whitbeck
 
CSS - OOCSS, SMACSS and more
CSS - OOCSS, SMACSS and moreCSS - OOCSS, SMACSS and more
CSS - OOCSS, SMACSS and more
Russ Weakley
 
Cascading Style Sheets - CSS
Cascading Style Sheets - CSSCascading Style Sheets - CSS
Cascading Style Sheets - CSS
Sun Technlogies
 
HTML, CSS and Java Scripts Basics
HTML, CSS and Java Scripts BasicsHTML, CSS and Java Scripts Basics
HTML, CSS and Java Scripts Basics
Sun Technlogies
 
Html Ppt
Html PptHtml Ppt
Html Ppt
vijayanit
 

Viewers also liked (20)

Php array
Php arrayPhp array
Php array
 
Php array
Php arrayPhp array
Php array
 
PHP Unit 4 arrays
PHP Unit 4 arraysPHP Unit 4 arrays
PHP Unit 4 arrays
 
Php array
Php arrayPhp array
Php array
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Synapseindia reviews on array php
Synapseindia reviews on array phpSynapseindia reviews on array php
Synapseindia reviews on array php
 
My self learn -Php
My self learn -PhpMy self learn -Php
My self learn -Php
 
03 Php Array String Functions
03 Php Array String Functions03 Php Array String Functions
03 Php Array String Functions
 
Array in php
Array in phpArray in php
Array in php
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros Developer
 
Web app development_php_06
Web app development_php_06Web app development_php_06
Web app development_php_06
 
PHP: Arrays
PHP: ArraysPHP: Arrays
PHP: Arrays
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
PHP Arrays
PHP ArraysPHP Arrays
PHP Arrays
 
Php ppt
Php pptPhp ppt
Php ppt
 
jQuery For Beginners - jQuery Conference 2009
jQuery For Beginners - jQuery Conference 2009jQuery For Beginners - jQuery Conference 2009
jQuery For Beginners - jQuery Conference 2009
 
CSS - OOCSS, SMACSS and more
CSS - OOCSS, SMACSS and moreCSS - OOCSS, SMACSS and more
CSS - OOCSS, SMACSS and more
 
Cascading Style Sheets - CSS
Cascading Style Sheets - CSSCascading Style Sheets - CSS
Cascading Style Sheets - CSS
 
HTML, CSS and Java Scripts Basics
HTML, CSS and Java Scripts BasicsHTML, CSS and Java Scripts Basics
HTML, CSS and Java Scripts Basics
 
Html Ppt
Html PptHtml Ppt
Html Ppt
 

Similar to PHP array 1

PHP array 2
PHP array 2PHP array 2
PHP array 2
Mudasir Syed
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
Amirul Azhar
 
Basics of array.pptx
Basics of array.pptxBasics of array.pptx
Basics of array.pptx
PRASENJITMORE2
 
Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4
Mohd Harris Ahmad Jaal
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
Skillspire LLC
 
Lists and arrays
Lists and arraysLists and arrays
Data structure in perl
Data structure in perlData structure in perl
Data structure in perl
sana mateen
 
Array andfunction
Array andfunctionArray andfunction
Array andfunction
Girmachew Tilahun
 
Web Technology - PHP Arrays
Web Technology - PHP ArraysWeb Technology - PHP Arrays
Web Technology - PHP Arrays
Tarang Desai
 
Mod 12
Mod 12Mod 12
Mod 12
obrienduke
 
C
CC
Scripting3
Scripting3Scripting3
Scripting3
Nao Dara
 
Php basics
Php basicsPhp basics
Php basics
hamfu
 
Unit 2-Arrays.pptx
Unit 2-Arrays.pptxUnit 2-Arrays.pptx
Unit 2-Arrays.pptx
mythili213835
 
Arrays
ArraysArrays
Arrays
Edwin Llamas
 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvvLecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
 
Chap6java5th
Chap6java5thChap6java5th
Chap6java5th
Asfand Hassan
 
Chapter 6 Absolute Java
Chapter 6 Absolute JavaChapter 6 Absolute Java
Chapter 6 Absolute Java
Shariq Alee
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbai
Vibrant Technologies & Computers
 
2 Arrays & Strings.pptx
2 Arrays & Strings.pptx2 Arrays & Strings.pptx
2 Arrays & Strings.pptx
aarockiaabinsAPIICSE
 

Similar to PHP array 1 (20)

PHP array 2
PHP array 2PHP array 2
PHP array 2
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
 
Basics of array.pptx
Basics of array.pptxBasics of array.pptx
Basics of array.pptx
 
Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4Web Application Development using PHP Chapter 4
Web Application Development using PHP Chapter 4
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Lists and arrays
Lists and arraysLists and arrays
Lists and arrays
 
Data structure in perl
Data structure in perlData structure in perl
Data structure in perl
 
Array andfunction
Array andfunctionArray andfunction
Array andfunction
 
Web Technology - PHP Arrays
Web Technology - PHP ArraysWeb Technology - PHP Arrays
Web Technology - PHP Arrays
 
Mod 12
Mod 12Mod 12
Mod 12
 
C
CC
C
 
Scripting3
Scripting3Scripting3
Scripting3
 
Php basics
Php basicsPhp basics
Php basics
 
Unit 2-Arrays.pptx
Unit 2-Arrays.pptxUnit 2-Arrays.pptx
Unit 2-Arrays.pptx
 
Arrays
ArraysArrays
Arrays
 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvvLecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
 
Chap6java5th
Chap6java5thChap6java5th
Chap6java5th
 
Chapter 6 Absolute Java
Chapter 6 Absolute JavaChapter 6 Absolute Java
Chapter 6 Absolute Java
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbai
 
2 Arrays & Strings.pptx
2 Arrays & Strings.pptx2 Arrays & Strings.pptx
2 Arrays & Strings.pptx
 

More from Mudasir Syed

Error reporting in php
Error reporting in php Error reporting in php
Error reporting in php
Mudasir Syed
 
Cookies in php lecture 2
Cookies in php  lecture  2Cookies in php  lecture  2
Cookies in php lecture 2
Mudasir Syed
 
Cookies in php lecture 1
Cookies in php lecture 1Cookies in php lecture 1
Cookies in php lecture 1
Mudasir Syed
 
Ajax
Ajax Ajax
Reporting using FPDF
Reporting using FPDFReporting using FPDF
Reporting using FPDF
Mudasir Syed
 
Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2
Mudasir Syed
 
Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2
Mudasir Syed
 
Filing system in PHP
Filing system in PHPFiling system in PHP
Filing system in PHP
Mudasir Syed
 
Time manipulation lecture 2
Time manipulation lecture 2Time manipulation lecture 2
Time manipulation lecture 2
Mudasir Syed
 
Time manipulation lecture 1
Time manipulation lecture 1 Time manipulation lecture 1
Time manipulation lecture 1
Mudasir Syed
 
Php Mysql
Php Mysql Php Mysql
Php Mysql
Mudasir Syed
 
Adminstrating Through PHPMyAdmin
Adminstrating Through PHPMyAdminAdminstrating Through PHPMyAdmin
Adminstrating Through PHPMyAdmin
Mudasir Syed
 
Sql select
Sql select Sql select
Sql select
Mudasir Syed
 
PHP mysql Sql
PHP mysql  SqlPHP mysql  Sql
PHP mysql Sql
Mudasir Syed
 
PHP mysql Mysql joins
PHP mysql  Mysql joinsPHP mysql  Mysql joins
PHP mysql Mysql joins
Mudasir Syed
 
PHP mysql Introduction database
 PHP mysql  Introduction database PHP mysql  Introduction database
PHP mysql Introduction database
Mudasir Syed
 
PHP mysql Installing my sql 5.1
PHP mysql  Installing my sql 5.1PHP mysql  Installing my sql 5.1
PHP mysql Installing my sql 5.1
Mudasir Syed
 
PHP mysql Er diagram
PHP mysql  Er diagramPHP mysql  Er diagram
PHP mysql Er diagram
Mudasir Syed
 
PHP mysql Database normalizatin
PHP mysql  Database normalizatinPHP mysql  Database normalizatin
PHP mysql Database normalizatin
Mudasir Syed
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functions
Mudasir Syed
 

More from Mudasir Syed (20)

Error reporting in php
Error reporting in php Error reporting in php
Error reporting in php
 
Cookies in php lecture 2
Cookies in php  lecture  2Cookies in php  lecture  2
Cookies in php lecture 2
 
Cookies in php lecture 1
Cookies in php lecture 1Cookies in php lecture 1
Cookies in php lecture 1
 
Ajax
Ajax Ajax
Ajax
 
Reporting using FPDF
Reporting using FPDFReporting using FPDF
Reporting using FPDF
 
Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2
 
Oop in php lecture 2
Oop in  php lecture 2Oop in  php lecture 2
Oop in php lecture 2
 
Filing system in PHP
Filing system in PHPFiling system in PHP
Filing system in PHP
 
Time manipulation lecture 2
Time manipulation lecture 2Time manipulation lecture 2
Time manipulation lecture 2
 
Time manipulation lecture 1
Time manipulation lecture 1 Time manipulation lecture 1
Time manipulation lecture 1
 
Php Mysql
Php Mysql Php Mysql
Php Mysql
 
Adminstrating Through PHPMyAdmin
Adminstrating Through PHPMyAdminAdminstrating Through PHPMyAdmin
Adminstrating Through PHPMyAdmin
 
Sql select
Sql select Sql select
Sql select
 
PHP mysql Sql
PHP mysql  SqlPHP mysql  Sql
PHP mysql Sql
 
PHP mysql Mysql joins
PHP mysql  Mysql joinsPHP mysql  Mysql joins
PHP mysql Mysql joins
 
PHP mysql Introduction database
 PHP mysql  Introduction database PHP mysql  Introduction database
PHP mysql Introduction database
 
PHP mysql Installing my sql 5.1
PHP mysql  Installing my sql 5.1PHP mysql  Installing my sql 5.1
PHP mysql Installing my sql 5.1
 
PHP mysql Er diagram
PHP mysql  Er diagramPHP mysql  Er diagram
PHP mysql Er diagram
 
PHP mysql Database normalizatin
PHP mysql  Database normalizatinPHP mysql  Database normalizatin
PHP mysql Database normalizatin
 
PHP mysql Aggregate functions
PHP mysql Aggregate functionsPHP mysql Aggregate functions
PHP mysql Aggregate functions
 

Recently uploaded

What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
EduSkills OECD
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
deepaannamalai16
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
nitinpv4ai
 
The basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptxThe basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptx
heathfieldcps1
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
zuzanka
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
melliereed
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
Mohammad Al-Dhahabi
 
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
indexPub
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
سمير بسيوني
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
JomonJoseph58
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
deepaannamalai16
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
danielkiash986
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
nitinpv4ai
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
Skimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S EliotSkimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S Eliot
nitinpv4ai
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 

Recently uploaded (20)

What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptxBeyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
Beyond Degrees - Empowering the Workforce in the Context of Skills-First.pptx
 
Standardized tool for Intelligence test.
Standardized tool for Intelligence test.Standardized tool for Intelligence test.
Standardized tool for Intelligence test.
 
Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10Haunted Houses by H W Longfellow for class 10
Haunted Houses by H W Longfellow for class 10
 
The basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptxThe basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptx
 
SWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptxSWOT analysis in the project Keeping the Memory @live.pptx
SWOT analysis in the project Keeping the Memory @live.pptx
 
Nutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour TrainingNutrition Inc FY 2024, 4 - Hour Training
Nutrition Inc FY 2024, 4 - Hour Training
 
skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)skeleton System.pdf (skeleton system wow)
skeleton System.pdf (skeleton system wow)
 
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
THE SACRIFICE HOW PRO-PALESTINE PROTESTS STUDENTS ARE SACRIFICING TO CHANGE T...
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdfمصحف القراءات العشر   أعد أحرف الخلاف سمير بسيوني.pdf
مصحف القراءات العشر أعد أحرف الخلاف سمير بسيوني.pdf
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
Stack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 MicroprocessorStack Memory Organization of 8086 Microprocessor
Stack Memory Organization of 8086 Microprocessor
 
HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.HYPERTENSION - SLIDE SHARE PRESENTATION.
HYPERTENSION - SLIDE SHARE PRESENTATION.
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
 
Bonku-Babus-Friend by Sathyajith Ray (9)
Bonku-Babus-Friend by Sathyajith Ray  (9)Bonku-Babus-Friend by Sathyajith Ray  (9)
Bonku-Babus-Friend by Sathyajith Ray (9)
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
Skimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S EliotSkimbleshanks-The-Railway-Cat by T S Eliot
Skimbleshanks-The-Railway-Cat by T S Eliot
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 

PHP array 1

  • 2. ARRYAS • Array is a data structure, which provides the facility to store a collection of data of same type under single variable name. • An array is a collection of data values, organized as an ordered collection of key- value pairs.
  • 3. INDEXED VERSUS ASSOCIATIVE ARRAYS • There are two kinds of arrays in PHP: • indexed and associative. • The keys of an indexed array are integers, beginning at 0. • Indexed arrays are used when you identify things by their position. • Associative arrays have strings as keys and behave more like two-column tables. • The first column is the key, which is used to access the value.
  • 4. INDEXED VERSUS ASSOCIATIVE ARRAYS • In both cases, the keys are unique--that is, you can't have two elements with the same key, regardless of whether the key is a string or an integer. • PHP arrays have an internal order to their elements that is independent of the keys and values, and there are functions that you can use to traverse the arrays based on this internal order. • The order is normally that in which values were inserted into the array.
  • 5. IDENTIFYING ELEMENTS OF AN ARRAY • You can access specific values from an array using the array variable's name, • followed by the element's key (sometimes called the index) within square brackets: • $age['Fred'] • $shows[2] • The key can be either a string or an integer. • String values that are equivalent to integer numbers (without leading zeros) are treated as integers. • Thus, $array[3] and $array['3'] reference the same element, but $array['03'] references a different element.
  • 6. IDENTIFYING ELEMENTS OF AN ARRAY • You don't have to quote single-word strings. • For instance, • $age['Fred'] is the same as $age[Fred]. • However, it's considered good PHP style to always use quotes, because quoteless keys are indistinguishable from constants.
  • 7. STORING DATA IN ARRAYS • Storing a value in an array will create the array if it didn't already exist. • For example: • echo $addresses[0]; // prints nothing • echo $addresses; // prints nothing • $addresses[0] = 'spam@cyberpromo.net'; • echo $addresses; // prints "Array" • $addresses[0] = 'spam@cyberpromo.net'; • $addresses[1] = 'abuse@example.com'; • $addresses[2] = 'root@example.com';
  • 8. STORING DATA IN ARRAYS • $price[ 'Gasket‘ ] = 15.29; • $price[ 'Wheel‘ ] = 75.25; • $price[ 'Tire‘ ] = 50.00; • An easier way to initialize an array is to use the array( ) construct, which builds an array from its arguments: • $addresses = array( 'spam@cyberpromo.net', 'abuse@example.com', 'root@example.com‘ );
  • 9. STORING DATA IN ARRAYS • To create an associative array with array( ), • use the => symbol to separate indexes from values: • $price = array( 'Gasket' => 15.29, • 'Wheel' => 75.25, • 'Tire' => 50.00); • $price = array( 'Gasket'=>15.29,'Wheel'=>75.25,'Tire'=>50.00); • To construct an empty array, pass no arguments to array( ): • $addresses = array( );
  • 10. STORING DATA IN ARRAYS • You can specify an initial key with => and then a list of values. • The values are inserted into the array starting with that key, with subsequent values having sequential keys: • $days = array(1 => 'Monday', 'Tuesday', 'Wednesday', • 'Thursday', 'Friday', 'Saturday', 'Sunday'); • // 2 is Tuesday, 3 is Wednesday, etc. • If the initial index is a non-numeric string, subsequent indexes are integers beginning at 0. • $whoops = array('Friday' => 'Black', 'Brown', 'Green'); • // same as • $whoops = array('Friday' => 'Black', 0 => 'Brown', 1 => 'Green');
  • 11. ADDING VALUES TO THE END OF AN ARRAY • To insert more values into the end of an existing indexed array, use the [] syntax: • $family = array('Fred', 'Wilma'); • $family[] = 'Pebbles'; // $family[2] is 'Pebbles' • This construct assumes the array's indexes are numbers and assigns elements into the next available numeric index, starting from 0. • Attempting to append to an associative array is almost always a programmer mistake, but PHP will give the new elements numeric indexes without issuing a warning: • $person = array('name' => 'Fred'); • $person[] = 'Wilma'; // $person[0] is now 'Wilma'
  • 12. VIEWING ARRAYS • You can see the structure and values of any array by using one of two functions — var_dump or print_r. • The print_r() statement, however, gives somewhat less information. • To display the $customers array, use the following functions: print_r($customers); • To get more information, use the following functions: var_dump($customers);
  • 13. MODIFYING ARRAY & STORING ONE ARRAY IN ANOTHER • Arrays can be changed at any time in the script, just as variables can. • The individual values can be changed, elements can be added or removed, and elements can be rearranged. • $customers[2] = “John”; • $customers[4] = “Hidaya”; • $customers[] = “Dell”; • You can also copy an entire existing array into a new array with this statement: • $customerCopy = $customers;
  • 14. REMOVING VALUES FROM ARRAYS • Sometimes you need to completely remove a value from an array. • $colors = array ( “red”, “green”, “blue”, “pink”, “yellow” ); • $colors[ 3 ] = “”; • Although this statement sets $colors[3] to blank, it does not remove it from the array. You still have an array with five values, one of the values being an empty string. • To totally remove the item from the array, you need to unset it. • unset($colors[3]);
  • 15. REMOVING VALUES FROM ARRAYS • Removing all the values doesn’t remove the array itself • To remove the array itself, you can use the following statement • unset($colors);
  • 16. USING ARRAYS IN STATEMENTS • Arrays can be used in statements in the same way that variables are used in. • You can retrieve any individual value in an array by accessing it directly, as in the following example: • $Sindhcapital = $capitals[‘Sindh’]; • echo $Sindhcapital; • You can echo an array value like this: • echo $capitals[‘Sindh’];
  • 17. GETTING THE SIZE OF AN ARRAY • The count( ) and sizeof( ) functions are identical in use and effect. • They return the number of elements in the array. • Here's an example: • $family = array('Fred', 'Wilma', 'Pebbles'); • $size = count($family); // $size is 3 • These functions do not consult any numeric indexes that might be present: • $confusion = array( 10 => 'ten', 11 => 'eleven', 12 => 'twelve'); • $size = count($confusion); // $size is 3
  • 18. WALKING THROUGH AN ARRAY • Walking through each and every element in an array, in order, is called iteration. • It is also sometimes called traversing. • Two ways to walk through an array: • Traversing an array manually: – Uses a pointer to move from one array value to another. • Using foreach: – Automatically walks through the array, from beginning to end, one value at a time.
  • 19. USING FOREACH TO WALK THROUGH AN ARRAY • You can use foreach to walk through an array one value at a time and execute a block of statements by using each value in the array. • The general format is as follows: foreach ( $arrayname as $keyname => $valuename ) { block of statements; }
  • 20. RANGE() • You can also create an array with a range of values by using the following function. $years = range(2001, 2010,[step]); • Here step is a positive number which has default value 1. Step is used for number of increment.
  • 21. LIST() • You can retrieve several values at once from an array with the list function. • The list function copies values from an array into variables. • $colors=array(“red”,”green”); list($red,$green)=$colors;
  • 22. ARRAY_SLICE() • You can split an array by creating a new array that contains a subset of an existing array. • Works same as substr works for string. • You can do this by using following function: $subArray = array_slice($arrayname,start,length);
  • 23. ARRAY_MERGE() • Conversely, you can merge two or more arrays together by using the following function: $bigArray = array_merge($array1,$array2,...);
  • 24. ARRAY_SUM() • To add all the values in an array, use the following function: • $sum = array_sum($array); • Of course, you are only going to add elements in an array of numbers. • PHP converts strings to 0 if you try to add them.
  • 25. ARRAY_UNIQUE() • Removes duplicate items from an array: $names2 = array_unique($names);
  • 26. ARRAY_FLIP() • To swap the keys and values, use the following function: $arrayFlipped = array_flip($testarray);
  • 27. extract() • Extracts array, forms variables with the names of keys or indexes of array. • extract($array);
  • 28. ASSIGMENTS 1. Make a linear Search using array. 2. Take an unsorted array and sort it using bubble sort technique. 3. Make an array which should contain intersected values of two given arrays.