# Merging Arrays

• wpas
• Joined: Jul 12, 2010
• Posts: 214
• Status: Offline

November 10th, 2010, 6:04 pm

I have two arrays:

Code: [ Select ]

array1 = ("one", "two", "three", "four");

array2 = (1, 2, 3, 4);
1. array1 = ("one", "two", "three", "four");
2. array2 = (1, 2, 3, 4);

If I merge the arrays, I get:

Code: [ Select ]

\$array3 = array_merge(\$array1, \$array2);
1. \$array3 = array_merge(\$array1, \$array2);

which gives:

Code: [ Select ]

Array
(
[0] => one
[1] => two
[2] => three
[3] => four
[4] => 1
[5] => 2
[6] => 3
[7] => 4
)
1. Array
2. (
3.   [0] => one
4.   [1] => two
5.   [2] => three
6.   [3] => four
7.   [4] => 1
8.   [5] => 2
9.   [6] => 3
10.   [7] => 4
11. )

Is it possible to merge the arrays so that the result is as follows:

Code: [ Select ]

Array
(
[0] => one 1
[1] => two 2
[2] => three 3
[3] => four 4

)
1. Array
2. (
3.   [0] => one 1
4.   [1] => two 2
5.   [2] => three 3
6.   [3] => four 4
7. )

[0] of array1 is merged with [0] of array2, etc.

I would also like to have a space to separate as shown

Thanks
http://www.schembrionics.com
The Ultimate Solutions Center
• Anonymous
• Bot
• Joined: 25 Feb 2008
• Posts: ?
• Loc: Ozzuland
• Status: Online

November 10th, 2010, 6:04 pm

• SpooF
• ٩๏̯͡๏۶
• Joined: May 22, 2004
• Posts: 3415
• Loc: Richland, WA
• Status: Offline

November 10th, 2010, 6:18 pm

Code: [ Select ]
\$var1 = "one";
\$var2 = 1;

\$var3 = \$var1 + " " + \$var2;

echo \$var3;
1. \$var1 = "one";
2. \$var2 = 1;
3. \$var3 = \$var1 + " " + \$var2;
4. echo \$var3;

Will give you "one 1"
#define NULL (::rand() % 2)
• wpas
• Joined: Jul 12, 2010
• Posts: 214
• Status: Offline

November 10th, 2010, 9:28 pm

Hi SpooF

I understand your concept but am having trouble trying to incorporate it.

As I indicated I have two arrays, \$array1 and \$array2

They are dynamically created which means elements are automatically added to each array making them bigger with time.

I a trying to use foreach loops to try and get what you mention but am not having any luck.

Could you get me started with the script

Thanks
http://www.schembrionics.com
The Ultimate Solutions Center
• Bogey
• Bogey
• Joined: Jul 14, 2005
• Posts: 8211
• Loc: USA
• Status: Offline

November 10th, 2010, 9:50 pm

PHP Code: [ Select ]
<?php
\$array1 = array("one", "two", "three", "four");
\$array2 = array(1, 2, 3, 4);
\$array3 = array();
for (\$i = 0; \$i < count(\$array1); \$i++) {
\$array3[\$i] = \$array1[\$i] . ' ' . \$array2[\$i];
}

echo '<pre>';
print_r(\$array3);
echo '</pre>';
?>
1. <?php
2. \$array1 = array("one", "two", "three", "four");
3. \$array2 = array(1, 2, 3, 4);
4. \$array3 = array();
5. for (\$i = 0; \$i < count(\$array1); \$i++) {
6.     \$array3[\$i] = \$array1[\$i] . ' ' . \$array2[\$i];
7. }
8.
9. echo '<pre>';
10. print_r(\$array3);
11. echo '</pre>';
12. ?>

That should work if both arrays are of equal lengths.

[EDIT] You can use the following one if you don't have equal lengths and if it's not important
PHP Code: [ Select ]
<?php
\$array1 = array("one", "two", "three", "four");
\$array2 = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
\$array3 = array();
\$c1 = count(\$array1);
\$c2 = count(\$array2);
\$count = (\$c1 > \$c2) ? \$c1 : \$c2;
for (\$i = 0; \$i < \$count; \$i++) {
\$array3[\$i] = trim(\$array1[\$i] . ' ' . \$array2[\$i]);
}

echo '<pre>';
print_r(\$array3);
echo '</pre>';
?>
1. <?php
2. \$array1 = array("one", "two", "three", "four");
3. \$array2 = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
4. \$array3 = array();
5. \$c1 = count(\$array1);
6. \$c2 = count(\$array2);
7. \$count = (\$c1 > \$c2) ? \$c1 : \$c2;
8. for (\$i = 0; \$i < \$count; \$i++) {
9.     \$array3[\$i] = trim(\$array1[\$i] . ' ' . \$array2[\$i]);
10. }
11.
12. echo '<pre>';
13. print_r(\$array3);
14. echo '</pre>';
15. ?>
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
• wpas
• Joined: Jul 12, 2010
• Posts: 214
• Status: Offline

November 10th, 2010, 10:16 pm

Hi Bogey

Worked like a charm, nice and easy

Thanks alot
http://www.schembrionics.com
The Ultimate Solutions Center

## Post Information

• Total Posts in this topic: 5 posts
• Users browsing this forum: No registered users and 128 guests
• You cannot post new topics in this forum
• You cannot reply to topics in this forum
• You cannot edit your posts in this forum
• You cannot delete your posts in this forum
• You cannot post attachments in this forum

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.