300 likes | 444 Views
ECA 236. Open Source Server Side Scripting PHP Array Functions. Sorting Functions. sort( ) // sorts by value, alphabetically, ascending // case sensitive – CAPS come before lc asort( ) // sorts 1D associative array by value ksort( ) // sorts 1D associative array by key.
E N D
ECA 236 Open Source Server Side Scripting PHP Array Functions Open Source Server Side Scripting
Sorting Functions • sort( ) // sorts by value, alphabetically, ascending // case sensitive – CAPS come before lc • asort( ) // sorts 1D associative array by value • ksort( ) // sorts 1D associative array by key asort( $parties ); ksort( $parties ); Open Source Server Side Scripting
Sorting Functions cont … Sorting in reverse order: • rsort( ) // sorts by value, alphabetically, descending // case sensitive – CAPS come before lc • arsort( ) // reverse sorts 1D associative array by value • krsort( ) // reverse sorts 1D associative array by key arsort( $parties ); krsort( $parties ); Open Source Server Side Scripting
Sorting Functions cont … Sorting Multidimensional Arrays: usort( ) • sorts an array by its values, using a user defined comparison function • takes 2 arguments • array to be sorted • call to the user defined comparison function • user defined function must return 0, 1, or -1 usort( $arrayName, ‘functionName’ ); Open Source Server Side Scripting
Sorting Functions cont … usort( ) cont … $halle = array( ‘Halle’, ‘Golden Retriever’, 63 ); $marley = array( ‘Marley’, ‘Border Collie’, 55 ); $boo = array( ‘Boo’, Golden Retriever’, 58 ); $dogs = array( $halle, $marley, $boo ); usort( $dogs, ‘comp’ ); function comp( $x, $y ){ if ( $x[ 0 ] = = $y[ 0 ] ){ return 0; } if ( $x[ 0 ] < $y[ 0 ] ){ return -1; } else return 1;} // end function comp Open Source Server Side Scripting
Sorting Functions cont … • uasort( ) sorts an array by values using a user defined comparison function • uksort( ) sorts an array by keys using a user defined comparison function Open Source Server Side Scripting
Sorting Functions cont … natsort( ) // sorts in natural order, not computer order $array1 = $array2 = array ("img12.png", "img10.png", "img2.png", "img1.png"); sort($array1);echo "Standard sorting\n";print_r($array1); natsort($array2);echo "\nNatural order sorting\n";print_r($array2); Standard SortingArray ( [0] => img1.png [1] => img10.png [2] => img12.png [3] => img2.png) Natural order sortingArray ( [3] => img1.png [2] => img2.png [1] => img10.png [0] => img12.png ) Open Source Server Side Scripting
Pointer Functions • current( ) // returns the value of the element currently being pointed to • reset( ) // returns the pointer to first element in the array • end( ) // sends the pointer to the end of the array • next( ) // advances the pointer forward one element – similar to each( ), except that next( ) advances the pointer and then returns the new current element, while each( ) returns the current element before advancing the pointer • prev( ) // moves the pointer back one element then returns the new current element Open Source Server Side Scripting
Reordering Functions • shuffle( ) randomly reorders the elements in an array • array_reverse( ) Creates a copy of the original array in reverse order • array_flip( ) Changes the array so that the keys and values switch places Open Source Server Side Scripting
pass by value $x = 10; $n = 13 incr_it( $x, $n ); function incr_it( $pass_x, $f ){ $pass_x = $pass_x * $f; } // $x is still 10 • Normally, when a value is passes to a function, a new variable is created • Any changes to the parameter inside the function definition do not affect the value of the original variable Open Source Server Side Scripting
pass by reference $x = 10; $n = 13 incr_it( $x, $n ); function incr_it( &$pass_x, $f ){ $pass_x = $pass_x * $f; } // $x is now 130 • Rather than creating a new variable, the function takes a reference to the original variable • Any changes to the variable inside the function also changes the original variable • Use an ampersand ( & ) to pass by reference Open Source Server Side Scripting
array_walk( ) • Use to manipulate or modify every element in a array • Like usort( ), array_walk( ) expects a user defined function • prototype of array_walk( )int array_walk ( array arr, string func, [ mixed userdata ] ) Open Source Server Side Scripting
array_walk( ) cont … • array_walk( ) takes three parameters: 1.the array to be processed 2.a user-defined function that will be applied to each element in the array 3.an optional third parameter which, if used, will be passed to the function as a parameter Open Source Server Side Scripting
array_walk( ) cont … • The user defined function can take 1 – 3 arguments.1. the value of the current array element 2. the key of the current array element 3.the optional third parameter • The structure looks like this:functionName ( $value, $key, $userdata ) Open Source Server Side Scripting
array_walk( ) cont … • EG, using one parameter array_walk( $parties, ‘printNewLine’ ); function printNewLine( $value ){ echo “$value<br />”; } Open Source Server Side Scripting
array_walk( ) cont … • EG, using two parameters array_walk( $parties , ‘printIt’ ); function printIt( $v, $k ){ echo “$v is a member of the $k Party”; } Open Source Server Side Scripting
Other Functions • array_keys( ) • used to pull the keys out of an array • takes one argument, the array name • returns a 2nd array, of the original array’s keys $newArray = array_keys( $parties ); Open Source Server Side Scripting
Other Functions cont … • array_values( ) • used to pull the values out of an array • takes one argument, the array name • returns a 2nd array, of the original array’s values $newArray = array_values( $parties ); Open Source Server Side Scripting
Other Functions cont … • array_key_exists( ) • returns TRUE if a specified key is present in an array • takes two arguments • the array name • the key to search for • array_sum( )sums the numeric values in an array array_key_exists( ‘Hippy’, $parties ); Open Source Server Side Scripting
Other Functions cont … • array_slice( ) • returns a slice of an array • ignores the keys of an associative array • takes three arguments1.array name 2.integer offset – the index starting point of the slice. If the offset is negative, the slice will start that far from the end of the array. 3.integer length – the length of the returned slice. If no length is provided, the function returns all elements from the offset to the end of the array. Open Source Server Side Scripting
Other Functions cont … • array_slice( ) cont … $input = array ("a", "b", "c", "d", "e"); $output = array_slice ($input, 2); // returns "c", "d", and "e“ $output = array_slice ($input, -2, 1); // returns "d“ $output = array_slice ($input, 0, 3); // returns "a", "b", and "c" Open Source Server Side Scripting
Other Functions cont … • array_push( ) • pushes or adds one or more values to end of array • takes two arguments • the array name • the values to be added to the end of the array • returns the new number of elements in the array array_push( $parties, “Nixon”, “Eagleton” ); Open Source Server Side Scripting
Other Functions cont … • array_unshift( ) • pushes or adds one or more values to the beginning of array • takes two arguments • the array name • the values to be added to the beginning of the array • returns the new number of elements in the array array_unshift( $parties, “Nixon”, “Eagleton” ); Open Source Server Side Scripting
Other Functions cont … • array_pop( ) • removes the element from the end of the array • returns value that was removed • array_shift( ) • removes the element from the beginning of the array • returns value that was removed array_pop( $parties ); array_shift( $parties ); Open Source Server Side Scripting
Other Functions cont … • array_map( ) • expects user-defined (callback) function • callback functions takes as many arguments as there are arrays to map • returns an array containing all elements of original arrays, after callback has been applied • array_map( ) takes 2 or more parameters: thecallback function, thearrays to be mapped $a = array( 1, 2, 3, 4, 5); $b = array_map( 'cube_it', $a ); function cube_it( $n ){ return( $n * $n * $n );} Open Source Server Side Scripting
Other Functions cont … • array_map( ) • passing null as first argument of array_map( ) creates a multi-dimensional array $a = array( 1,2,3,4,5); $b = array( 'one','two','three','four','five' ); $c = array("uno","dos","tres","cuatro","cinco"); $d = array_map( null, $a, $b, $c ); Open Source Server Side Scripting
Other Functions cont … • is_array( ) returns TRUE is a variable is an array • extract( ) creates scalar variables out of the keys in an array – the values of those variables are set to the values in the array $myArray = ( ‘k1’ => ‘Marley’, ‘k2’ => ‘Halle’, ‘k3’ => ‘Boo’ ); extract( $myArray ); echo “$k1, $k2, $k3”; produces the output: Marley, Halle, Boo Open Source Server Side Scripting
Transformations • implode( ) • turns an array into a string • takes two arguments • separator or delimiter • array to be imploded $myString = implode( “ | “, $parties ); // creates the following string $myString = “Democratic|Republican|Reform|Libertarian|Hippy|Natural Law”; Open Source Server Side Scripting
Transformations cont … • explode( ) • turns a string into an array • takes two arguments • separator or delimiter • string to be exploded $myString = “Democratic|Republican|Reform|Libertarian|Hippy|Natural Law”; $myArray = explode( “ | “, $myString ); // assigns the original array to $myArray Open Source Server Side Scripting
Other Important Arrays • superglobal arrays $_SERVER $_GET $_POST $_COOKIE $_FILES $_ENV $_REQUEST $_SESSION Open Source Server Side Scripting