Jump to content


Photo

Merge Arrays


  • Please log in to reply
1 reply to this topic

#1 vidyashankara

vidyashankara
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 23 June 2006 - 08:25 PM

I have the following arrays

Array
(
    [0] => 2  A1 B1 C1 X       

    [1] => 2  A2 B2 C3 Y         

    [2] => 2  A3 B3 C3 Z       

    [3] => 3  D1 E1 F1 A        

    [4] => 3   D2 E2 F2 B           

    [5] => 3   D3 E3 F3 C           

    [6] => 4  G1 H1 I1 J        

    [7] => 4  G2 H2 I2 K       

    [8] => 4  G3 H3 I3 L   

)


How do i merge it to output the following?
Array
(
    [0] => 2  A1 B1 C1 X     A2 B2 C3   Y      A3 B3 C3 Z      

    [1] => 3  D1 E1 F1 A     D2 E2 F2 B      D3 E3 F3 C   

    [2] => 4   G1 H1 I1 J     G2 H2 I2 K    G3 H3 I3 L   
}


Is this possible? Please help me out. I was thinking of the following code
$count = $array[0];
if (!array_unique($count)) {
array_merge($array)
}


Doesnt work, i dont know how to define the arrays to merge...


#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 26 June 2006 - 02:09 PM

You don't have more than one array there, you have a single array. But to get the result you require, you could probably go about it like so...

<?php
$array = array("2  A1 B1 C1 X","2  A2 B2 C3 Y","2  A3 B3 C3 Z",
"3  D1 E1 F1 A","3  D2 E2 F2 B","3  D3 E3 F3 C",
"4  G1 H1 I1 J","4  G2 H2 I2 K","4  G3 H3 I3 L");

$i = 0;
$new = array();
$tmp = "";
foreach($array as $a) {
    $tmp .= "$a\t";
    $i++;

    if($i%3==0) {
        $new[] = $tmp;
        $tmp = "";
    }
}
echo "<pre>"; print_r($new); echo "</pre>";
?>
Might need a little tweaking, but it's along the right path.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users