Webmaster Forum
Go Back   Webmaster Forums UK SEO SEM Webmaster Community Forum - UKWW > Web Design and Website Development > Programming > PHP Forum
Register FAQ Members List Downloads Calendar Today's Posts Webmaster Resources Webmaster Blogs

UK Web Hosting
UK Web Hosting
Website Hosting
Website Hosting
UK One Way
UK One Way
Get Top 10 Exposure
 
Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-09-2008, 07:33 PM
dman_2007
Guest
 
Posts: n/a
iTrader: / %
Default Tip : merging arrays in php

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);
?>
Thisis the output this code produces :

Quote:
Array ( [name] => joe [0] => 1 [1] => 2 [2] => a [3] => b [4] => c )
Since both $array1 and $array2 contains elements with 'name' key, value from the later array i.e. $array2 overwrites the value from previous array i.e. $array1. Also take a look at the numeric keys, both array have elements with numeric key 0, but none of them overwrites the other. Both of them are present in the resultant array with the final key based on their overall position in the sequence of array elements with numeric keys from all the input arrays. Note that even though we specifically assigned numeric key 5 to the value 1 in the first array and there is no other element in any array with the same key, but still it has been given a new key based on its position.

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);
?>
This is what the resultant array will look like :

Quote:
Array ( [name] => matthew [5] => 1 [6] => 2 [0] => a [1] => b [2] => c )
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
  #2 (permalink)  
Old 05-28-2008, 07:25 AM
garima's Avatar
Junior Member
 
Join Date: May 2008
Posts: 17
iTrader: 0 / 0%
garima is on a distinguished road
Default

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.
Digg this Post!Add Post to del.icio.usStumble this Post!Wong this Post!
Reply With Quote
Reply

Bookmarks

Webmaster Resources
UK Web Hosting
UK WW SEO Tools
Free site submission
Web Directory
 
Advertisement
firewall script
 
Site Of the Month
BizzFace
Nominate site of the month
 
Tag Cloud
adsense ads space buy banners changing host changing server channel solutions cheap off page packages cheap seo packages cheap seo services closing coupon code boss cart jv directory announcement domain name extension domain names e brochures ecommerce flash website promotion flash websites forum free directory google host quack hostquack inline price changes introduction ipn paypal payment boss job site joomla job link bid script hungarian link exchange linux host moving server off page seo packages picture of the day referral sell baners seo seo packages shared hosting submit url switching host thumbnail tld today in history traffic transfering host uk shipping postcodes web design webinare webmaster tools wordpress

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump



All times are GMT. The time now is 06:25 AM.

UK Webmaster World Forums - Internet marketing, web development, domain names, SEO contest and discussuons.
Subscribe to our feeds   Subscribe to our feeds

Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149