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( ):
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;
}

More Related Content

What's hot

Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
Vineet Kumar Saini
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
Ahmed Swilam
 
Marc’s (bio)perl course
Marc’s (bio)perl courseMarc’s (bio)perl course
Marc’s (bio)perl course
Marc Logghe
 
Arrays in PHP
Arrays in PHPArrays in PHP
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
Amirul Azhar
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
mussawir20
 
Introduction to perl_control structures
Introduction to perl_control structuresIntroduction to perl_control structures
Introduction to perl_control structures
Vamshi Santhapuri
 
Perl
PerlPerl
Arrays in php
Arrays in phpArrays in php
Arrays in php
Laiby Thomas
 
Lists and arrays
Lists and arraysLists and arrays
Array in php
Array in phpArray in php
Array in php
ilakkiya
 
Php array
Php arrayPhp array
Php array
Core Lee
 
Array in php
Array in phpArray in php
Array in php
Ashok Kumar
 
Introduction to perl_lists
Introduction to perl_listsIntroduction to perl_lists
Introduction to perl_lists
Vamshi Santhapuri
 
DBIx::Class introduction - 2010
DBIx::Class introduction - 2010DBIx::Class introduction - 2010
DBIx::Class introduction - 2010
leo lapworth
 
DBIx::Class beginners
DBIx::Class beginnersDBIx::Class beginners
DBIx::Class beginners
leo lapworth
 
Arrays
ArraysArrays
Arrays
Edwin Llamas
 

What's hot (17)

Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Class 4 - PHP Arrays
Class 4 - PHP ArraysClass 4 - PHP Arrays
Class 4 - PHP Arrays
 
Marc’s (bio)perl course
Marc’s (bio)perl courseMarc’s (bio)perl course
Marc’s (bio)perl course
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Array String - Web Programming
Array String - Web ProgrammingArray String - Web Programming
Array String - Web Programming
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
 
Introduction to perl_control structures
Introduction to perl_control structuresIntroduction to perl_control structures
Introduction to perl_control structures
 
Perl
PerlPerl
Perl
 
Arrays in php
Arrays in phpArrays in php
Arrays in php
 
Lists and arrays
Lists and arraysLists and arrays
Lists and arrays
 
Array in php
Array in phpArray in php
Array in php
 
Php array
Php arrayPhp array
Php array
 
Array in php
Array in phpArray in php
Array in php
 
Introduction to perl_lists
Introduction to perl_listsIntroduction to perl_lists
Introduction to perl_lists
 
DBIx::Class introduction - 2010
DBIx::Class introduction - 2010DBIx::Class introduction - 2010
DBIx::Class introduction - 2010
 
DBIx::Class beginners
DBIx::Class beginnersDBIx::Class beginners
DBIx::Class beginners
 
Arrays
ArraysArrays
Arrays
 

Viewers also liked

Javascript lecture 3
Javascript lecture 3Javascript lecture 3
Javascript lecture 3
Mudasir Syed
 
Javascript 2
Javascript 2Javascript 2
Javascript 2
Mudasir Syed
 
Css presentation lecture 1
Css presentation lecture 1Css presentation lecture 1
Css presentation lecture 1
Mudasir Syed
 
Java script lecture 1
Java script lecture 1Java script lecture 1
Java script lecture 1
Mudasir Syed
 
Web forms and html lecture Number 4
Web forms and html lecture Number 4Web forms and html lecture Number 4
Web forms and html lecture Number 4
Mudasir Syed
 
String functions and operations
String functions and operations String functions and operations
String functions and operations
Mudasir Syed
 
Css presentation lecture 3
Css presentation lecture 3Css presentation lecture 3
Css presentation lecture 3
Mudasir Syed
 
Dreamweaver cs6
Dreamweaver cs6Dreamweaver cs6
Dreamweaver cs6
Mudasir Syed
 
Dom in javascript
Dom in javascriptDom in javascript
Dom in javascript
Mudasir Syed
 
String functions and operations
String functions and operations String functions and operations
String functions and operations
Mudasir Syed
 
Sessions in php
Sessions in php Sessions in php
Sessions in php
Mudasir Syed
 
Css presentation lecture 4
Css presentation lecture 4Css presentation lecture 4
Css presentation lecture 4
Mudasir Syed
 
Functions in php
Functions in phpFunctions in php
Functions in php
Mudasir Syed
 
String functions and operations
String functions and operations String functions and operations
String functions and operations
Mudasir Syed
 
Form validation server side
Form validation server side Form validation server side
Form validation server side
Mudasir Syed
 
Javascript lecture 4
Javascript lecture  4Javascript lecture  4
Javascript lecture 4
Mudasir Syed
 
Form validation with built in functions
Form validation with built in functions Form validation with built in functions
Form validation with built in functions
Mudasir Syed
 
Web forms and html lecture Number 3
Web forms and html lecture Number 3Web forms and html lecture Number 3
Web forms and html lecture Number 3
Mudasir Syed
 
loops and branches
loops and branches loops and branches
loops and branches
Mudasir Syed
 
introduction to programmin
introduction to programminintroduction to programmin
introduction to programmin
Mudasir Syed
 

Viewers also liked (20)

Javascript lecture 3
Javascript lecture 3Javascript lecture 3
Javascript lecture 3
 
Javascript 2
Javascript 2Javascript 2
Javascript 2
 
Css presentation lecture 1
Css presentation lecture 1Css presentation lecture 1
Css presentation lecture 1
 
Java script lecture 1
Java script lecture 1Java script lecture 1
Java script lecture 1
 
Web forms and html lecture Number 4
Web forms and html lecture Number 4Web forms and html lecture Number 4
Web forms and html lecture Number 4
 
String functions and operations
String functions and operations String functions and operations
String functions and operations
 
Css presentation lecture 3
Css presentation lecture 3Css presentation lecture 3
Css presentation lecture 3
 
Dreamweaver cs6
Dreamweaver cs6Dreamweaver cs6
Dreamweaver cs6
 
Dom in javascript
Dom in javascriptDom in javascript
Dom in javascript
 
String functions and operations
String functions and operations String functions and operations
String functions and operations
 
Sessions in php
Sessions in php Sessions in php
Sessions in php
 
Css presentation lecture 4
Css presentation lecture 4Css presentation lecture 4
Css presentation lecture 4
 
Functions in php
Functions in phpFunctions in php
Functions in php
 
String functions and operations
String functions and operations String functions and operations
String functions and operations
 
Form validation server side
Form validation server side Form validation server side
Form validation server side
 
Javascript lecture 4
Javascript lecture  4Javascript lecture  4
Javascript lecture 4
 
Form validation with built in functions
Form validation with built in functions Form validation with built in functions
Form validation with built in functions
 
Web forms and html lecture Number 3
Web forms and html lecture Number 3Web forms and html lecture Number 3
Web forms and html lecture Number 3
 
loops and branches
loops and branches loops and branches
loops and branches
 
introduction to programmin
introduction to programminintroduction to programmin
introduction to programmin
 

Similar to PHP array 2

Data structure in perl
Data structure in perlData structure in perl
Data structure in perl
sana mateen
 
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
 
Web Technology - PHP Arrays
Web Technology - PHP ArraysWeb Technology - PHP Arrays
Web Technology - PHP Arrays
Tarang Desai
 
Array andfunction
Array andfunctionArray andfunction
Array andfunction
Girmachew Tilahun
 
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
 
Basics of array.pptx
Basics of array.pptxBasics of array.pptx
Basics of array.pptx
PRASENJITMORE2
 
Unit 2-Arrays.pptx
Unit 2-Arrays.pptxUnit 2-Arrays.pptx
Unit 2-Arrays.pptx
mythili213835
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
Skillspire LLC
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbai
Vibrant Technologies & Computers
 
Chap6java5th
Chap6java5thChap6java5th
Chap6java5th
Asfand Hassan
 
Chapter 6 Absolute Java
Chapter 6 Absolute JavaChapter 6 Absolute Java
Chapter 6 Absolute Java
Shariq Alee
 
PHP-04-Arrays.ppt
PHP-04-Arrays.pptPHP-04-Arrays.ppt
PHP-04-Arrays.ppt
Leandro660423
 
Learn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & ArraysLearn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & Arrays
Eng Teong Cheah
 
Mod 12
Mod 12Mod 12
Mod 12
obrienduke
 
Chapter 2 wbp.pptx
Chapter 2 wbp.pptxChapter 2 wbp.pptx
Chapter 2 wbp.pptx
40NehaPagariya
 
2 Arrays & Strings.pptx
2 Arrays & Strings.pptx2 Arrays & Strings.pptx
2 Arrays & Strings.pptx
aarockiaabinsAPIICSE
 
Php basics
Php basicsPhp basics
Php basics
hamfu
 
Arrays in php
Arrays in phpArrays in php
Arrays in php
soumyaharitha
 
05php
05php05php
C
CC

Similar to PHP array 2 (20)

Data structure in perl
Data structure in perlData structure in perl
Data structure in perl
 
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
 
Web Technology - PHP Arrays
Web Technology - PHP ArraysWeb Technology - PHP Arrays
Web Technology - PHP Arrays
 
Array andfunction
Array andfunctionArray andfunction
Array andfunction
 
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
 
Basics of array.pptx
Basics of array.pptxBasics of array.pptx
Basics of array.pptx
 
Unit 2-Arrays.pptx
Unit 2-Arrays.pptxUnit 2-Arrays.pptx
Unit 2-Arrays.pptx
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbai
 
Chap6java5th
Chap6java5thChap6java5th
Chap6java5th
 
Chapter 6 Absolute Java
Chapter 6 Absolute JavaChapter 6 Absolute Java
Chapter 6 Absolute Java
 
PHP-04-Arrays.ppt
PHP-04-Arrays.pptPHP-04-Arrays.ppt
PHP-04-Arrays.ppt
 
Learn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & ArraysLearn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & Arrays
 
Mod 12
Mod 12Mod 12
Mod 12
 
Chapter 2 wbp.pptx
Chapter 2 wbp.pptxChapter 2 wbp.pptx
Chapter 2 wbp.pptx
 
2 Arrays & Strings.pptx
2 Arrays & Strings.pptx2 Arrays & Strings.pptx
2 Arrays & Strings.pptx
 
Php basics
Php basicsPhp basics
Php basics
 
Arrays in php
Arrays in phpArrays in php
Arrays in php
 
05php
05php05php
05php
 
C
CC
C
 

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

How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
Celine George
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
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
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
imrankhan141184
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
paigestewart1632
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
S. Raj Kumar
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
Nguyen Thanh Tu Collection
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
Israel Genealogy Research Association
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
Wahiba Chair Training & Consulting
 

Recently uploaded (20)

How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
How to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRMHow to Manage Your Lost Opportunities in Odoo 17 CRM
How to Manage Your Lost Opportunities in Odoo 17 CRM
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
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
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
Traditional Musical Instruments of Arunachal Pradesh and Uttar Pradesh - RAYH...
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
Cognitive Development Adolescence Psychology
Cognitive Development Adolescence PsychologyCognitive Development Adolescence Psychology
Cognitive Development Adolescence Psychology
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
 
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
BÀI TẬP DẠY THÊM TIẾNG ANH LỚP 7 CẢ NĂM FRIENDS PLUS SÁCH CHÂN TRỜI SÁNG TẠO ...
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
The Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collectionThe Diamonds of 2023-2024 in the IGRA collection
The Diamonds of 2023-2024 in the IGRA collection
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience How to Create a More Engaging and Human Online Learning Experience
How to Create a More Engaging and Human Online Learning Experience
 

PHP array 2

  • 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( ):
  • 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; }