Merging Arrays

  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

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
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

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"
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

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
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

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. ?>
  • wpas
  • Proficient
  • Proficient
  • User avatar
  • Posts: 322
  • Loc: Canada

Post 3+ Months Ago

Hi Bogey

Worked like a charm, nice and easy

Thanks alot

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 122 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.