spacer
Web Development Tutorials PHP Tutorials
 Developer Newsletter

Tutorials
AJAX
ASP
CGI & Perl
CSS
Flash
HTML
Illustrator
Java
JavaScript
Linux
MySQL
PHP
Photoshop
Python
Wireless
XML
Miscellaneous


Scripts Directory
AJAX Scripts
ASP Scripts
ASP.NET Scripts
CGI & Perl Scripts
Flash Scripts
Java Scripts
JavaScript Scripts
PHP Scripts
Python Scripts
Remotely Hosted Scripts
Tools & Utilities Scripts
XML Scripts

Web Hosting Directory
ASP.NET
Budget
Dedicated Servers
Ecommerce
Linux
Resellers
Shared
Small Business
Windows

Developer Manuals
Learn HTML
Learn PHP
Learn CSS
Learn AJAX
Learn JavaScript
Learn Pear
Free White Papers

Developer Resources
Developer Tools
Developer Content
Survey Software
Dedicated Servers




Introduction to PHP Programming

By PHP Catalyst
2007-11-19


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.

Tutorial Pages:
» What can I do with PHP?
» Popoular Features of PHP
» Basics of PHP
» Variables in PHP
» Data Types in PHP
» Expressions and Operators
» Control Structures in PHP
» Functions in PHP
» Declaring Functions in PHP
» Scope of a Variable
» Built-in Functions in PHP
» Handling Strings in PHP
» Printing Strings in PHP
» String Comparisons in PHP
» Manipulating Strings in PHP
» Arrays in PHP
» Types of Arrays in PHP
» Creating Arrays in PHP
» Array Operations in PHP


 | Bookmark Print |   Write For Us
Related Tutorials:
» Web Database Access from Desktop Applications
» CubeCart 3.0 Installation and Configuration
» PHP Site Search Made Easy
» Installing and Configuring Drupal 6.1
» Desktop Application Development with PHP-GTK
» Installing PHP on Windows



About the NetVisits, Inc Network | Write For Us | Advertise
Copyright ©2007 NetVisits, Inc Network. All Rights Reserved. Privacy Policy.
Visit other NetVisits, Inc. sites: