Page 143 - Open Soource Technologies 304.indd
P. 143
Unit 6: Arrays
If you pad an associative array, existing keys will be preserved. New elements will have numeric Notes
keys starting at 0.
Self Assessment
Choose the correct answer:
1. An array with a numeric index is called:
(a) Associative array (b) Creative array
(c) Indexed array (d) None of these
2. The array that uses named keys that you assign to them is known as:
(a) Associative array (b) Creative array
(c) Indexed array (d) None of these
3. Storing a value in an array will create the array if it did not already exist.
(a) True (b) False
4. The range( ) function is used to:
(a) create an array of consecutive integer
(b) get the length of array
(c) get the size of array
(d) none of these
6.4 Array Operations in PHP
In this we will see some most commonly used array functions, their usage with examples.
6.4.1 Split an Array into Chunks
To split an array into smaller chunks or smaller sized arrays, we use array_chunk() function of
PHP. This will return arrays of several smaller sizes and each array’s index number will start
with zero unless you want to use the preserve_keys parameter to preserve the original index
numbers from the input array used. The syntax is:
array_chunk ( array input, int size [, bool preserve_keys] )
Using the above function in an example:
Example:
<?php
$my_array = array(“One”, “Two”, “Three”, “Four”);
$split_array = array_chunk($my_array, 2);
print_r($split_array);
//Output: Array (
// [0] => Array (
// [0] => One
// [1] => Two )
// [1] => Array (
LOVELY PROFESSIONAL UNIVERSITY 137