Jump to content

Create a Table from pairs of array


doc1355
 Share

Go to solution Solved by Barand,

Recommended Posts

I have an array as below: 

Array
(
    [0] => Array
        (
            [u1] => 6
            [u2] => 4
        )

    [1] => Array
        (
            [u1] => 6
            [u2] => 3
        )

    [2] => Array
        (
            [u1] => 3
            [u2] => 4
        )

    [3] => Array
        (
            [u1] => 3
            [u2] => 11
        )

    [4] => Array
        (
            [u1] => 3
            [u2] => 11
        )

    [5] => Array
        (
            [u1] => 3
            [u2] => 4
        )

    [6] => Array
        (
            [u1] => 4
            [u2] => 11
        )

    [7] => Array
        (
            [u1] => 4
            [u2] => 11
        )

    [8] => Array
        (
            [u1] => 6
            [u2] => 3
        )

    [9] => Array
        (
            [u1] => 6
            [u2] => 4
        )

    [10] => Array
        (
            [u1] => 6
            [u2] => 11
        )

    [11] => Array
        (
            [u1] => 6
            [u2] => 11
        )

)

What I'm trying to achieve is to create a table as below: 

6:  4   3  3  4  11   11

4:  6   3  3  11  11   6

3:  6   4  11  11  4   6 

11: 3   3   4   4  6   6 

 

Here is what is happening: 

It shows the first value of u1 (6) , then searches all the other arrays, if that value (6) exists, it shows the other value one at a time ( 4-3-3-4-11-11). Then starts new row, looks up the next value (which is not same as the first value and goes through the same process, until we have one row for all the unique numbers in the array, then stops. 

I'm not sure how I can setup my loop to do this and I appreciate any helps that anyone could offer. 

 

Thank you. 

 

Link to comment
Share on other sites

Create an intermediate array which restructures your data

foreach ($array as $a) {
    if (!isset($new[$a['u1']])) {
        $new[$a['u1']] = [];
    }
    $new[$a['u1']][] = $a['u2'];
}

which gives $new array ...

Array
(
    [6] => Array
        (
            [0] => 4
            [1] => 3
            [2] => 3
            [3] => 4
            [4] => 11
            [5] => 11
        )

    [3] => Array
        (
            [0] => 4
            [1] => 11
            [2] => 11
            [3] => 4
        )

    [4] => Array
        (
            [0] => 11
            [1] => 11
        )

)

Now it's simple to loop through outputting the rows and columns.

  • Like 1
Link to comment
Share on other sites

Posted (edited)

Thank you so much for your quick reply. This is a great help. 

By using the loop with the $new variable I can show half of the table (blue) only and not the other half (red). I know this is duplicate of the blue data . How can I show the second half (red) in my loop to make it a full table?

6:  4   3  3  4  11   11

4:  6   3  3  11  11   6

3:  6   4  11  11  4   6 

11: 3   3   4   4  6   6 

Edited by doc1355
Link to comment
Share on other sites

  • Solution

Rinse and repeat - exchanging u1 and u2

$new = [];
foreach ($array as $a) {
    if (!isset($new[$a['u1']])) {
        $new[$a['u1']] = [];
    }
    $new[$a['u1']][] = $a['u2'];
    //repeat exchanging u1 and u2
    if (!isset($new[$a['u2']])) {
        $new[$a['u2']] = [];
    }
    $new[$a['u2']][] = $a['u1'];
}
//
//  Output $new array
//
echo '<pre>';
foreach ($new as $u1 => $u2s) {
    printf('<br><b>%4d</b> | ', $u1);
    foreach ($u2s as $u) {
        printf('%4d &vellip;', $u);
    }
}  

image.png.1048b22dd4992ab6841fb6842cb4aeb7.png

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

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.

 Share

×
×
  • 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.