KEMBAR78
Working with arrays in php | PPTX
Working with arrays
Array Basics
Looping Through Arrays
Modifying Arrays
Sorting Arrays
Using Multidimensional Arrays
Array Basics
• Arrays are collections of related values
• the data submitted from a form,
• the names of students in a class,
• the populations of a list of cities.
• An array is a container that holds multiple values, each distinct from
the rest.
• Made up of elements
• Each elements has key and value
• Only one element with the given key
• Arrays and other non-scalar values can’t be keys but they can be
elements
• Element value can be strings, numbers, true or false or can be other
array
Creating arrays
Creating arrays with array( )
Choosing a good array name
• Follow same rules as variables names
• Scalars and array cannot have the same names
• $a cannot be of both scalar and array type
Numeric array
• Arrays with numeric keys
• Also called indexed or ordered array
• Keys signify the position
Creating a numeric array
Adding elements with [ ]
Finding the Size of an Array
Looping through arrays
• foreach()
• works only on arrays
• used to loop through each key/value pair
• Syntax
• foreach ($array as $value) {
code to be executed;
}
• for()
• used when you know in advance how many times the script should run
• Syntax
• for (init counter; test counter; increment counter) {
code to be executed;
}
Looping with foreach()
Alternating table rows colors
Modifying an array with foreach()
Using foreach( ) with numeric arrays
Iterating through a numeric array with for( )
Alternating table row colors with for( )
Array element order and foreach( )
• To guarantee that elements are accessed in numerical key order, use
for( )
Checking for an element with a particular key
Checking for an element with a particular
value
Modifying Arrays
• Operating on array elements
• Interpolating array element values in double-quoted strings
• Interpolating array element values with curly braces
• Making a string from an array with implode( )
• Printing HTML table rows with implode( )
• Turning a string into an array with explode( )
Operating on array elements
Interpolating array element values in double-
quoted strings
Interpolating array element values with curly
braces
unset()
implode
• The implode() function returns a string from the elements of an array.
• Syntax
• implode(separator,array)
Making a string from an array with implode( )
Printing HTML table rows with implode( )
Turning a string into an array with explode( )
Sorting Arrays
• Sorting with sort( )
• Sorting with asort( )
• Sorting with ksort( )
• Sorting with arsort( )
Sorting with sort( )
Sorting with asort( )
Sorting with ksort( )
• rsort( )
• arsort( )
• krsort( )
• Sort the arrays in descending order or reverse alphabetical order
Sorting with arsort( )
Using Multidimensional Array
• Creating multidimensional arrays with array( )
• Accessing multidimensional array elements
• Manipulating multidimensional arrays
• Iterating through a multidimensional array with foreach( )
• Iterating through a multidimensional array with for( )
Creating multidimensional arrays with array( )
Accessing multidimensional array elements
Manipulating multidimensional arrays
Iterating through a multidimensional array
with foreach( )
Iterating through a multidimensional array
with for( )
Working with arrays in php

Working with arrays in php