Webmaster

References : PHP 4.0


Array handling

Functions within this category will allow you to handle arrays. In PHP, an array is a structure that allows you to map values to keys for many essential data handling operations, such as storing, sorting, listing, queuing, etc. The compound type used to declare such a structure is simply array.

array(value1, value2...) | array_walk(array, function) | arsort(unsorted_array) | asort(unsorted_array) | count(variable) | current(array) | end(array) | explode(separator, string) | implode(array, separator) | key(array) | ksort(array) | list(value1, value2...) | max(array) | max(value1, value2...) | min(array) | min(value1, value2...) | next(array) | prev(array) | reset(array) | rsort(unsorted_array) | sort(unsorted_array) | uasort(unsorted_array, function) | uksort(unsorted_array, function) | usort(unsorted_array, function)

array(value1, value2...)
Enables you to obtain an array from a list of values specified as arguments. Note that the => operator can be used to define an index-value pair. However, this is optional. If the index is omitted, an automatic element numbering will take place, starting with an index of 0. In our example, we start the array indexing at 2.
 
Example$fr_flag = array(2=>"blue", "white", "red");
 
 Top
array_walk(array, function)
Applies the user-defined function referenced by the second argument to each element of the array named by the first argument. It cannot be a native PHP function.
 
Example<?
  $fr_flag = array("blue", "white", "red");
?>
 
 Top
arsort(unsorted_array)
Sorts an array by values in descending order, while keeping the key associations.
 
Example<?
  $group = array("k1"=>"Car1",
    "k2"=>"Car2",
    "k3"=>"Car3");

  arsort($group);

  for(reset($group); $index=key($group); next($group))
  {
      print("$index: $group[$index] <br>\n");
  }
?>
 
 Top
asort(unsorted_array)
Sorts an array by values in ascending order.
 
Example<?
  $group = array("k1"=>"Car1",
    "k2"=>"Car2",
    "k3"=>"Car3");

  asort($group);

  for(reset($group); $index=key($group); next($group))
  {
      print("$index: $group[$index] <br>\n");
  }
?>
 
 Top
count(variable)
Returns the number of elements in the variable. It returns 1 if the argument does not refer to an array and 0 if the variable has no value, i.e. NULL.
 
Example<?
  $fr_flag = array("blue", "white", "red");
  count($fr_flag);
?>
 
 Top
current(array)
An array has an internal pointer which indicates the current element. After its creation, it is initialised to the first element. This function enables you to obtain the value of the element pointed to. Note that a read by index does not move the pointer, nor does this function.
 
Example<?
  $fr_flag = array("blue", "white", "red");
  current($fr_flag);
?>
 
 Top
end(array)
Moves the pointer to the last element of the array named by the argument and returns its value.
 
Example<?
  $fr_flag = array("blue", "white", "red");
  end($fr_flag);
?>
 
 Top
explode(separator, string)
Returns an array of sub-strings extracted from the string given as the argument, using the first argument as the boundary of each sub-string.
 
Example<?
  $colours = "blue\nwhite\nred";
  $fr_flag = explode("\n", $colours);

?>
 
 Top
implode(array, separator)
Returns a string made up of the values of the array argument, inserting the second argument as a separator between values.
 
Example<?
  $fr_flag = array("blue", "white", "red");
  $fr_flag = implode($fr_flag, ",");

?>
 
 Top
key(array)
Returns the index or the named key of the element currently referenced by the internal pointer.
 
Example<?
  $group = array("k1"=>"Car1",
    "k2"=>"Car2",
    "k3"=>"Car3");

  for(reset($group); $key = key($group); next($group))
  {
      print("$key for $group [$key]<br>\n");
  }
?>
 
 Top
ksort(array)
Sorts an array by key values. This is particularly useful with an associative array.
 
Example<?
  $group = array("k1"=>"Car1",
    "k2"=>"Car2",
    "k3"=>"Car3");

  ksort($group);

  for(reset($group); $index=key($group); next($group))
  {
      print("$index: $group[$index] <br>\n");
  }
?>
 
 Top
list(value1, value2...)
This is not really a function, but more a language construct. It is used to assign values to several variables in one shot.
 
Example<?
  $fr_flag = array("blue", "white", "red");

  list($key, $value) = each($fr_flag);
?>
 
 Top
max(array)
Returns the element of the named array containing the highest numerical value. Note that this function can also be used to compare two or more values of other types.
 
Example<?
  $group = array("k1"=>"Car1",
    "k2"=>"Car2",
    "k3"=>"Car3");

  max($group);
?>
 
 Top
max(value1, value2...)
Compares the given arguments and returns the one with the highest value. When this function is not used with an array, it must be passed at least two arguments.
 
Example<? print(max(18, 89, 73, 155.7, 14) . "<br>\n"); ?>
 
 Top
min(array)
Returns the element of the named array containing the lowest value. It can be used with values of other types.
 
Example<?
  $group = array("k1"=>"Car1",
    "k2"=>"Car2",
    "k3"=>"Car3");

  min($group);
?>
 
 Top
min(value1, value2...)
Compares the passed arguments and returns the one with the highest value. When this function is not used with an array, it must be passed at least two arguments.
 
Example<? print(min(1, 89, 73, 155.7, 14)); ?>
 
 Top
next(array)
Moves the internal pointer of the named array to the element following the current one and returns its value. Returns FALSE if the current element was the last one.
 
Example<?
  $fr_flag = array("blue", "white", "red");
  next($fr_flag);
?>
 
 Top
prev(array)
Moves the internal pointer of the named array to the element preceding the current one and returns its value. Returns FALSE if the current element was the first one.
 
Example<?
  $fr_flag = array("blue", "white", "red");
  prev($fr_flag)
?>
 
 Top
reset(array)
Sets the internal pointer of the named array to its first element.
 
Example<?
  $fr_flag = array("blue", "white", "red");
  reset($fr_flag);
?>
 
 Top
rsort(unsorted_array)
Sorts the values of the named array in descending order. Note that this function deletes the current indexing scheme and a new zero-based indexing takes place.
 
Example<?
  $fr_flag = array("blue", "white", "red");
  rsort($fr_flag);
?>
 
 Top
sort(unsorted_array)
Sorts the values of the named array in ascending order. Note that this function deletes the current indexing scheme and a new zero-based indexing takes place.
 
Example<?
  $fr_flag = array("blue", "white", "red");
  sort($fr_flag);
?>
 
 Top
uasort(unsorted_array, function)
Sorts the values of the named array with a user-defined comparison function that has already been declared, while maintaining the key-value correlation. So this function is particularly useful for sorting associative arrays. The comparison function must return an integer less than, greater than or equal to zero to indicate if one of the two compared values is lower or higher than the other, or if they are equal.
 
Example<?
  function compare($element1, $element2)
  {
    return($element1 - $element2);
  }

  $ar_digit = array(7,4,6,2,5,9,8,3);

  uasort($ar_digit, "compare");

?>
 
 Top
uksort(unsorted_array, function)
Sorts the elements of the named array by key values, with a user-defined comparison function that has already been declared. The comparison function must return an integer less than, greater than or equal to zero to indicate if one of the two compared values is lower or higher than the other, or if they are equal.
 
Example<?
  function compare($element1, $element2)
  {
      return($element1 - $element2);

  $ar_digit = array(6=>"six",2=>"two",
    4=>"four",1=>"one",
    3=>"three",5=>"five");

  uksort($ar_digit, "compare");

?>
 
 Top
usort(unsorted_array, function)
Sorts the values of the named array with a user-defined comparison function that has already been declared. The comparison function must return an integer less than, greater than or equal to zero to indicate if one of the two compared values is lower or higher than the other, or if they are equal. Note that this function does not maintain the current indexes or named keys; a new zero-based indexing takes place.
 
Example<?
  function compare($element1, $element2)
  {
    return($element1 - $element2);
  }

  $ar_digit = array(3,4,6,2,5,7,8,9);

  usort($ar_digit, "compare");

?>
 
 Top