User Tools

Site Tools


uphp:functions:array

array

WMPRO, WMMINI FW >= 1.0 WMMEGA FW >= 2.0

Create an array, with optional values

Description

array array ( [ mixed $value [, ...] ] )

Parameters

$value(s): Optional comma separated list of values of any type (string, int, float, array)

Return Values

Array: empty or with values as specified

Examples

Creating Arrays

<? 
  $arr=array(); // create a blank array
  $arr[0]='Item 1';
  $arr[]='Item 2'; // automatically appends the value to the next available key
  $arr[2]='Item 3';
 
  $arr2=array(1,2,3,4); // array of integers
 
  $arr3=array("one",2,3.33333,$arr); // mixed array, elements of type: string, integer, float, array
?>

Key/Value Pairs

<pre><? 
  $arr=array(); // create a blank array
  $arr['name']='John';
  $arr['age']=33; 
 
  // iterate through the array and print out all keys and their associated values
  for ($i=0; $i < sizeof($arr); $i++) {
    $key=array_key($arr,$i);
    print("Key at Index ".$i." is ".$key." and value is ".$arr[$key]."\r\n");
  }
?></pre>

The above example will output:

Key at Index 0 is name and value is John
Key at Index 1 is age and value is 33

Note

An indexed array is a more efficient way to store variables in memory if the size and data type is known beforehand. The array() function creates a hashed array, which consumes much more memory per element.

See Also

indexed_array() - Create an array of a specific type and size

sizeof() - Return the number of elements in an array

print_r() - Dump the contents of an array to the current output

array_key() - Return the key for an array index

array_keys() - Return keys for an array that has key/value pairs

uPHP Variable Types and Limits

uphp/functions/array.txt · Last modified: 2021/09/13 05:57 (external edit)