This shows you the differences between two versions of the page.
uphp:functions:array [2017/03/27 20:44] jeff |
uphp:functions:array [2021/09/13 05:57] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ======array====== | ||
- | < | ||
- | |||
- | Create an array with values | ||
- | |||
- | ====Description==== | ||
- | <well size=" | ||
- | <span style=" | ||
- | < | ||
- | <span style=" | ||
- | <span style=" | ||
- | <span style=" | ||
- | </ | ||
- | |||
- | Return an array object | ||
- | |||
- | ====Parameters==== | ||
- | < | ||
- | |||
- | ====Return Value==== | ||
- | Array of values | ||
- | |||
- | ====Examples==== | ||
- | **Creating Arrays:** | ||
- | |||
- | <code php> | ||
- | <? | ||
- | $arr=array(); | ||
- | $arr[0]=' | ||
- | $arr[]=' | ||
- | $arr[2]=' | ||
- | |||
- | $arr2=array(1, | ||
- | | ||
- | $arr3=array(" | ||
- | ?> | ||
- | </ | ||
- | |||
- | **Key/Value Pairs:** | ||
- | |||
- | <code php> | ||
- | < | ||
- | $arr=array(); | ||
- | $arr[' | ||
- | $arr[' | ||
- | |||
- | // iterate through the array and print out all keys and their associated values | ||
- | for ($i=0; $i < sizeof($arr); | ||
- | $key=array_key($arr, | ||
- | print(" | ||
- | } | ||
- | ?></ | ||
- | </ | ||
- | |||
- | 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: | ||