Useful functions for arrays in php

4,822 views
4,468 views

Published on

Very Useful functions for arrays in php

1 Comment
1 Like
Statistics
Notes
No Downloads
Views
Total views
4,822
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
96
Comments
1
Likes
1
Embeds 0
No embeds

No notes for slide

Useful functions for arrays in php

  1. 1. Useful functions for arrays in PHP Function Explanation Examplesizeof($arr) This function returns the number Code: of elements in an array. $data = array("red", "green", "blue"); Use this function to find out how echo "Array has " . sizeof($data) . " many elements an array contains; elements"; this information is most commonly ?> used to initialize a loop counter when processing the array. Output: Array has 3 elementsarray_values($arr) This function accepts a PHP array Code: and returns a new array containing $data = array("hero" => "Holmes", "villain" only its values (not its keys). Its => "Moriarty"); counterpart is the array_keys() print_r(array_values($data)); function. ?> Use this function to retrieve all the Output: values from an associative array. Array ( [0] => Holmes [1] => Moriarty )array_keys($arr) This function accepts a PHP array Code: and returns a new array containing $data = array("hero" => "Holmes", "villain" only its keys (not its values). Its => "Moriarty"); counterpart is the array_values() print_r(array_keys($data)); function. ?> Use this function to retrieve all the Output: keys from an associative array. Array ( [0] => hero [1] => villain )array_pop($arr) This function removes an element Code: from the end of an array. $data = array("Donald", "Jim", "Tom"); array_pop($data); print_r($data);
  2. 2. ?> Output: Array ( [0] => Donald [1] => Jim )array_push($arr, $val) This function adds an element to Code: the end of an array. $data = array("Donald", "Jim", "Tom"); array_push($data, "Harry"); print_r($data); ?> Output: Array ( [0] => Donald [1] => Jim [2] => Tom [3] => Harry )array_shift($arr) This function removes an element Code: from the beginning of an array. $data = array("Donald", "Jim", "Tom"); array_shift($data); print_r($data); ?> Output: Array ( [0] => Jim [1] => Tom )array_unshift($arr, $val) This function adds an element to Code: the beginning of an array. $data = array("Donald", "Jim", "Tom"); array_unshift($data, "Sarah"); print_r($data); ?> Output: Array (
  3. 3. [0] => Sarah [1] => Donald [2] => Jim [3] => Tom )each($arr) This function is most often used to Code: iteratively traverse an array. Each $data = array("hero" => "Holmes", "villain" time each() is called, it returns the => "Moriarty"); current key-value pair and moves while (list($key, $value) = each($data)) { the array cursor forward one echo "$key: $value n"; element. This makes it most } suitable for use in a loop. ?> Output: hero: Holmes villain: Moriartysort($arr) This function sorts the elements of Code: an array in ascending order. String $data = array("g", "t", "a", "s"); values will be arranged in sort($data); ascending alphabetical order. print_r($data); ?> Note: Other sorting functions include asort(), arsort(), ksort(), Output: krsort() and rsort(). Array ( [0] => a [1] => g [2] => s [3] => t )array_flip($arr) The function exchanges the keys Code: and values of a PHP associative $data = array("a" => "apple", "b" => array. "ball"); print_r(array_flip($data)); Use this function if you have a ?> tabular (rows and columns) structure in an array, and you want Output: to interchange the rows and Array columns. ( [apple] => a [ball] => b )
  4. 4. array_reverse($arr) The function reverses the order of Code: elements in an array. $data = array(10, 20, 25, 60); print_r(array_reverse($data)); Use this function to re-order a ?> sorted list of values in reverse for easier processing—for example, Output: when youre trying to begin with Array the minimum or maximum of a set ( of ordered values. [0] => 60 [1] => 25 [2] => 20 [3] => 10 )array_merge($arr) This function merges two or more Code: arrays to create a single composite $data1 = array("cat", "goat"); array. Key collisions are resolved in $data2 = array("dog", "cow"); favor of the latest entry. print_r(array_merge($data1, $data2)); ?> Use this function when you need to combine data from two or more Output: arrays into a single structure—for Array example, records from two ( different SQL queries. [0] => cat [1] => goat [2] => dog [3] => cow )array_rand($arr) This function selects one or more Code: random elements from an array. $data = array("white", "black", "red"); echo "Todays color is " . Use this function when you need $data[array_rand($data)]; to randomly select from a ?> collection of discrete values—for example, picking a random color Output: from a list. Todays color is redarray_search($search, This function searches the values Code:$arr) in an array for a match to the $data = array("blue" => "#0000cc", "black" search term, and returns the => "#000000", "green" => "#00ff00"); corresponding key if found. If more echo "Found " . array_search("#0000cc", than one match exists, the key of $data); the first matching value is ?> returned. Output:
  5. 5. Use this function to scan a set of Found blue index-value pairs for matches, and return the matching index.array_slice($arr, This function is useful to extract a Code:$offset, $length) subset of the elements of an array, $data = array("vanilla", "strawberry", as another array. Extracting begins "mango", "peaches"); from array offset $offset and print_r(array_slice($data, 1, 2)); continues until the array slice is ?> $length elements long. Output: Use this function to break a larger Array array into smaller ones—for ( example, when segmenting an [0] => strawberry array by size ("chunking") or type [1] => mango of data. )array_unique($data) This function strips an array of Code: duplicate values. $data = array(1,1,4,6,7,4); print_r(array_unique($data)); Use this function when you need ?> to remove non-unique elements Output: from an array—for example, when Array creating an array to hold values for ( a tables primary key. [0] => 1 [3] => 6 [4] => 7 [5] => 4 )array_walk($arr, $func) This function "walks" through an Code: array, applying a user-defined function reduceBy10(&$val, $key) { function to every element. It $val -= $val * 0.1; returns the changed array. } Use this function if you need to $data = array(10,20,30,40); perform custom processing on array_walk($data, reduceBy10); every element of an array—for print_r($data); example, reducing a number series ?> by 10%. Output: Array ([0] => 9 [1] => 18 [2] => 27 [3] => 36 )

×