Jump to content

Merge Arrays


vidyashankara

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.