Array Operations in PHP

In this section we will see some most commonly used array functions, their usage with examples.

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:

<?
$my_array 
= array("One""Two""Three""Four");
$split_array array_chunk($my_array2);
print_r($split_array);
//Output: Array ( 
//                [0] => Array ( 
//                              [0] => One 
//                              [1] => Two ) 
//                [1] => Array ( 
//                              [0] => Three 
//                              [1] => Four ) 
//                               ) 
?>

Combine an array with data elements and other with its keys

We can create an array by combining one array with keys and second array with corresponding data elements. Note that the number of keys and data elements in both the arrays has to be equal for this operation to be successful. We will make use of built-in function array_combine(). Its syntax is:

array_combine ( array keys, array values )

and the example using array_combine() is:

<?
$keys_array 
= array(1,2,3,4);
$data_array = array("one","two","three","four");
$new_array array_combine($keys_array$data_array);
print_r($new_array);
//Output: Array
// (
//     [1]  => one
//     [2]  => two
//     [3]  => three
//     [4]  => four
// )
?>

Merging two or more arrays

Using an built-in function array_merge() we can merge two or more arrays to form a single array. The values from the second array are appended at the end of first array and so on. So, if three arrays are to be merged, elements from third will be appended at the end of second array and then it will be appended at the end of the first array. Take a look at this syntax and example:

array_merge ( array array1 [, array array2 [, array ...]] )
<?
$first_array 
= array(1,2);
$second_array = array(3,4);
$third_array = array(5,6);

$merged_array array_merge($first_array,$second_array,$third_array);
print_r($merged_array);
//Output: Array 
//          ( [0] => 1 
//            [1] => 2 
//            [2] => 3 
//            [3] => 4 
//            [4] => 5 
//            [5] => 6 
//          ) 
?>

Searching an array

Searching for a value in an array is made simple using the function array_search(). If the keyword is found in the array, then the corresponding key of that value is retured for further operations. The syntax and example are:

array_search ( keyword, array name )
<?
$months 
= array(1=>"Jan""Feb""Mar""Apr""May""June""July""Aug",
"Sep""Oct""Nov""Dec");
$key array_search("May"$months); //Searching for May in months array
echo $key;
// We can also print the value that key points at
echo $months[$key];
?>

With that example we wrap up our section on Arrays in PHP. If you feel there should be more content and examples or something else that should be on here, please contact us.