Jump to content

Archived

This topic is now archived and is closed to further replies.

vidyashankara

Merge Arrays

Recommended Posts

I have the following arrays

[code]
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  

)
[/code]


How do i merge it to output the following?
[code]
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  
}

[/code]

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

[/code]

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

Share this post


Link to post
Share on other sites
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...

[code]<?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>";
?>[/code]
Might need a little tweaking, but it's along the right path.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.