![]() |
|
|||||||
| Register | FAQ | Members List | Downloads | Calendar | Today's Posts | Search | Webmaster Resources | Webmaster Blogs |
![]() UK Web Hosting |
![]() Website Hosting |
![]() UK One Way |
|
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
||||
|
If you want to merge mulitple arrays into one array in php, you can do it either by using array_merge function or by using + operator. Each of these methods handle elements with duplicate keys differently.
1) array_merge function When using array_merge function, if more then one input array has same string keys, then the later value for that key from later input array will overwrite values from previous arrays. However, if more then one array contain same numeric keys, later value will not overwrite previous values. Instead, the resultant array will contain all the values taken sequentially from first array to last array indexed in a continuous way. This is the case even if the arrays do not contain any value with same numeric key.For example, take a look at the following code Code:
<?php
$array1 = array('name' => 'matthew', 5 => 1, 2);
$array2 = array("a", "b", "c", 'name' => 'joe');
$array3 = array_merge($array1, $array2);
print_r($array3);
?>
Quote:
2) + operator + operator is relatively simple, when merging arrays unlike array_merge function no reindexing will be done i.e. keys value will be preserved for each array element and when elements with same keys are encountered in later arrays, they will be ignored. For example, when the same arrays which were used in previous example are merged using + operator instead of array_merge function : Code:
<?php
$array1 = array('name' => 'matthew', 5 => 1, 2);
$array2 = array("a", "b", "c", 'name' => 'joe');
$array3 = $array1 + $array2;
print_r($array3);
?>
Quote:
|
|
|||||
|
Merging two arrays together is a one step command called array_merge.
<?php $pantry_food = array_merge ($pantry, $pantry2); ?> __________________________________________________ ________ Web site Design Last edited by garima; 05-28-2008 at 08:29 AM. |
![]() |
| Bookmarks |
| Webmaster Resources |
| • UK Web Hosting • UK WW SEO Tools • Free site submission • Web Directory |
| Advertisement |
![]() |
| Site Of the Month |
![]() Nominate site of the month |
|
|
| UK Webmaster World Forums - Internet marketing, web development, domain names, SEO contest and discussuons. |
| Subscribe to our feeds |