Oct
14
2008

ZCE Array: Array Functions

Theirs lots of array functions, I hope your ready for the ones that are listed in the Zend Guide. Their are many more that I have not included. Fingers crossed they dont appear in the zend test.

Sorting

array_reverse($array); will reorder so if element values where A C B they will now be in order B C A.

sort($array); will order element values alphabeticly (a, b, c) but destroys keys and re-key them from 0.
asort($array); will order elements values alphabaticaly (a, b, c) it wont destroy keys.
sort($array, sort_string); will order by comparing elements as strings. sort_numeric will sort by converting element to numeric values. If left normal it will sort byte by byte 10c is lower value than 2c for example, grr.

rsort($array); will order element values (c,b,a) and destroys keys.
arsort($array); will order elements values (c,b,a) it wont destroy keys.

natsort($array); will order element values alphabeticly (a,b,c) and 10c is higher then 2c. The keys stay intact

ksort($array); will order element keys alphabeticly (a,b,c)
usort($array, ‘function’); useing usort you can pass array to pre-made function for ordering. It destroys the keys so you can use:
uasort($array, ‘function’); pre-made function ordering but wont destroy keys.
shuffle($array); ANTI-SORT! this will re-order the array in random order. The keys are destroyed.

array_keys($array); this will will create a new array with just the values of the keys! so they can be re-applied later.
array_rand($array, 2); this will return 2 random key values from the array. So it can be used to select any random array values.

Arrays as Stacks, Queues and Sets

array_push($stack, ‘val1′, ‘val2′); is the same as $array[]=”val1″; and latter should be used. It ads the elements to the end of the array.
array_pop($stack); will output the last element in the array and remove it form contents of original array.
array_shift($stack); similar to array_pop but instead of last element, it will output and remove the first.
array_unshift($stack, ‘whereami’); will add element whereami as first element, it also increases the keys so it can fit.

Comparing Arrays

var_dump($a==$b); compares arrays a and b and will return true if they have the same number of elements with same values and keys in any order.
var_dump($a===$b); does exactly the same, except they have to be in the correct order.
array_diff($a,$b); will output the elements in $a that are not in $b.. If theirs a element in b thats not in a then it is not shown, or elements that are the same are also not shown.
array_diff_key($a,$b); will output difference between a and b with the same keys
array_diff_assoc($a,$b); will output difference between a and be key and value of the element.
array_intersect($a,$b); will output the elements in a and b that are the same.
arrray_interesect_key($a,$b); will output similarities between a and b with the same keys
array_intersect_assoc($a,$b); will output similarities between a and be key and value of the element.

Other Functions

count($a); will count how many elements are in array a.
is_array($a); will return true of false if a is an array.
array_key_exists(’element key’, $array); will return true if “element key” is name of the key in a associative array.
in_array(’element value’, $array); will return true if “element value” is contained within any element in the array.
unset($array['key']); will remove element with the key “key” from the array.
array_flip($array); will swap the key and value over, resulting in the value of an element being the value of the key.
array_walk($array, ‘function’); this will pass the key and values of an array to a function (they can be manipulated if function is associated &$value, &$key)
array_walk_recursive($array, ‘function’); same as above function with exception with it works with arrays within arrays.

Array Pointer Functions

Arrays have a pointer which indicates the current element of an array in a iteration.
reset($array); will reset pointer to initial position.
end($array); will reset pointer to the end position (last element)
prev($array); move pointer backwards by one position.
next($array); move pointer farwards by one position.
current($array); will return the value of the element that the pointer is on
key($array); will return the key of the element that the pointer is on.
The use of these functions useualy arraise in a loop where you need control of the output of arrays.

None array related by I Found a interesting link today:
Zend PHP 5 Certification - Watch Outs

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • LinkedIn
  • Live
  • StumbleUpon
  • Technorati
  • TwitThis

No Comments »

RSS feed for comments on this post. TrackBack URL

Leave a comment

WordPress Powered, Theme by TheBuckmaker.com | Add to Technorati Favorites. | RSS and Comments RSS