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 (20)

PPTX
Constructor and destructor
Shubham Vishwambhar
 
PPTX
Dynamic memory allocation in c++
Tech_MX
 
PPTX
Constructor in java
Madishetty Prathibha
 
PDF
jQuery for beginners
Arulmurugan Rajaraman
 
PDF
Applets
Prabhakaran V M
 
PDF
Singly linked list
Amar Jukuntla
 
PPT
Introduction to java beans
Hitesh Parmar
 
PPTX
Operators php
Chandni Pm
 
PDF
Methods in Java
Jussi Pohjolainen
 
PDF
Web Development Course: PHP lecture 1
Gheyath M. Othman
 
PPT
Java And Multithreading
Shraddha
 
PPTX
Data types in php
ilakkiya
 
PPTX
graphics programming in java
Abinaya B
 
PDF
Life cycle-of-a-thread
javaicon
 
PDF
JAVA PROGRAMMING - The Collections Framework
Jyothishmathi Institute of Technology and Science Karimnagar
 
PPTX
Array in Java
Ali shah
 
PDF
Servlet and servlet life cycle
Dhruvin Nakrani
 
PDF
What is Socket Programming in Python | Edureka
Edureka!
 
PPTX
Constructor in java
Hitesh Kumar
 
Constructor and destructor
Shubham Vishwambhar
 
Dynamic memory allocation in c++
Tech_MX
 
Constructor in java
Madishetty Prathibha
 
jQuery for beginners
Arulmurugan Rajaraman
 
Singly linked list
Amar Jukuntla
 
Introduction to java beans
Hitesh Parmar
 
Operators php
Chandni Pm
 
Methods in Java
Jussi Pohjolainen
 
Web Development Course: PHP lecture 1
Gheyath M. Othman
 
Java And Multithreading
Shraddha
 
Data types in php
ilakkiya
 
graphics programming in java
Abinaya B
 
Life cycle-of-a-thread
javaicon
 
JAVA PROGRAMMING - The Collections Framework
Jyothishmathi Institute of Technology and Science Karimnagar
 
Array in Java
Ali shah
 
Servlet and servlet life cycle
Dhruvin Nakrani
 
What is Socket Programming in Python | Edureka
Edureka!
 
Constructor in java
Hitesh Kumar
 

Viewers also liked (20)

PPT
Php array
Core Lee
 
PPSX
Php array
argusacademy
 
PDF
PHP Unit 4 arrays
Kumar
 
PPT
Arrays in PHP
Compare Infobase Limited
 
PPT
Synapseindia reviews on array php
saritasingh19866
 
ODP
My self learn -Php
laavanyaD2009
 
PPT
03 Php Array String Functions
Geshan Manandhar
 
PPTX
Array in php
ilakkiya
 
PPT
Oops in PHP By Nyros Developer
Nyros Technologies
 
PDF
Web app development_php_06
Hassen Poreya
 
PPT
PHP: Arrays
Mario Raul PEREZ
 
PDF
Arrays in PHP
Vineet Kumar Saini
 
PDF
PHP Arrays
Mahesh Gattani
 
PPT
Php ppt
Sanmuga Nathan
 
PPT
jQuery For Beginners - jQuery Conference 2009
Ralph Whitbeck
 
PDF
CSS - OOCSS, SMACSS and more
Russ Weakley
 
PPTX
Cascading Style Sheets - CSS
Sun Technlogies
 
PPTX
HTML, CSS and Java Scripts Basics
Sun Technlogies
 
PPT
Html Ppt
vijayanit
 
PPT
Php Presentation
Manish Bothra
 
Php array
Core Lee
 
Php array
argusacademy
 
PHP Unit 4 arrays
Kumar
 
Synapseindia reviews on array php
saritasingh19866
 
My self learn -Php
laavanyaD2009
 
03 Php Array String Functions
Geshan Manandhar
 
Array in php
ilakkiya
 
Oops in PHP By Nyros Developer
Nyros Technologies
 
Web app development_php_06
Hassen Poreya
 
PHP: Arrays
Mario Raul PEREZ
 
Arrays in PHP
Vineet Kumar Saini
 
PHP Arrays
Mahesh Gattani
 
jQuery For Beginners - jQuery Conference 2009
Ralph Whitbeck
 
CSS - OOCSS, SMACSS and more
Russ Weakley
 
Cascading Style Sheets - CSS
Sun Technlogies
 
HTML, CSS and Java Scripts Basics
Sun Technlogies
 
Html Ppt
vijayanit
 
Php Presentation
Manish Bothra
 
Ad

Similar to PHP array 1 (20)

PPT
PHP array 2
Mudasir Syed
 
PPTX
Chapter 2 wbp.pptx
40NehaPagariya
 
PPT
Arrays in php
Laiby Thomas
 
PPTX
Unit 2-Arrays.pptx
mythili213835
 
PPTX
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
 
PPT
Web Technology - PHP Arrays
Tarang Desai
 
PPTX
Web Application Development using PHP Chapter 4
Mohd Harris Ahmad Jaal
 
PPTX
5 Arry in PHP.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
ayushmishraaa09
 
PDF
4.1 PHP Arrays
Jalpesh Vasa
 
PPT
PHP - Introduction to PHP Arrays
Vibrant Technologies & Computers
 
PPTX
Working with arrays in php
Kamal Acharya
 
PPT
PHP-04-Arrays.ppt
Leandro660423
 
PDF
Array String - Web Programming
Amirul Azhar
 
PPTX
PHP Arrays_Introduction
To Sum It Up
 
PPTX
Php arrays
1crazyguy
 
PPT
Class 4 - PHP Arrays
Ahmed Swilam
 
PPT
Using arrays with PHP for forms and storing information
Nicole Ryan
 
DOCX
Array andfunction
Girmachew Tilahun
 
PPTX
PHP Array Functions.pptx
KirenKinu
 
PPTX
Introduction to php 6
pctechnology
 
PHP array 2
Mudasir Syed
 
Chapter 2 wbp.pptx
40NehaPagariya
 
Arrays in php
Laiby Thomas
 
Unit 2-Arrays.pptx
mythili213835
 
Lecture 5 array in PHP.pptxLecture 10 CSS part 2.pptxvvvvvvvvvvvvvv
ZahouAmel1
 
Web Technology - PHP Arrays
Tarang Desai
 
Web Application Development using PHP Chapter 4
Mohd Harris Ahmad Jaal
 
5 Arry in PHP.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
ayushmishraaa09
 
4.1 PHP Arrays
Jalpesh Vasa
 
PHP - Introduction to PHP Arrays
Vibrant Technologies & Computers
 
Working with arrays in php
Kamal Acharya
 
PHP-04-Arrays.ppt
Leandro660423
 
Array String - Web Programming
Amirul Azhar
 
PHP Arrays_Introduction
To Sum It Up
 
Php arrays
1crazyguy
 
Class 4 - PHP Arrays
Ahmed Swilam
 
Using arrays with PHP for forms and storing information
Nicole Ryan
 
Array andfunction
Girmachew Tilahun
 
PHP Array Functions.pptx
KirenKinu
 
Introduction to php 6
pctechnology
 
Ad

More from Mudasir Syed (20)

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

Recently uploaded (20)

PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
Horarios de distribución de agua en julio
pegazohn1978
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 

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] = '[email protected]'; • echo $addresses; // prints "Array" • $addresses[0] = '[email protected]'; • $addresses[1] = '[email protected]'; • $addresses[2] = '[email protected]';
  • 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( '[email protected]', '[email protected]', '[email protected]‘ );
  • 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.