Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a theoretical question that I cannot seem to figure out. Imagine I have the following data in a database:

Main    Sub1   Sub2
a       x      y
x       t      u
u       f      g

I want to make a multidimensional array in PHP/mYSQL by essentially asking "what is each 'main' component made of?"

The result would be something like this:

Array
   (
   [0] => a
     (
     [0] => x
       (
       [0] => t
       [1] => u
       )
         (
         [0] => f
         [1] => g
         )
     [1] => y
     )
   )

My efforts result in lots of arrays, instead of a multidimensional array.

share|improve this question
add comment

1 Answer

You can use references to solve this, although the results will get a bit messy:

$res = [];

foreach ($rows as $row) {
    // check if we have each sub component
    if (!isset($res[$row['sub1']])) {
        $res[$row['sub1']] = $row['sub1'];
    }
    if (!isset($res[$row['sub2']])) {
        $res[$row['sub2']] = $row['sub2'];
    }
    // build new component with references to the sub components
    $res[$row['main']] = [&$res[$row['sub1']], &$res[$row['sub2']]];
}

print_r($res);

Output

Array
(
    [x] => Array
        (
            [0] => t
            [1] => Array
                (
                    [0] => f
                    [1] => g
                )

        )

    [y] => y
    [a] => Array
        (
            [0] => Array
                (
                    [0] => t
                    [1] => Array
                        (
                            [0] => f
                            [1] => g
                        )

                )

            [1] => y
        )

    [t] => t
    [u] => Array
        (
            [0] => f
            [1] => g
        )

    [f] => f
    [g] => g
)

You can clean up the results by filtering out only the arrays:

print_r(array_filter($res, 'is_array'));

Output

Array
(
    [x] => Array
        (
            [0] => t
            [1] => Array
                (
                    [0] => f
                    [1] => g
                )

        )

    [a] => Array
        (
            [0] => Array
                (
                    [0] => t
                    [1] => Array
                        (
                            [0] => f
                            [1] => g
                        )

                )

            [1] => y
        )

    [u] => Array
        (
            [0] => f
            [1] => g
        )

)
share|improve this answer
    
Do you mind including the database retrieval syntax (e.g., while ($row = mysql_fetch_assoc($result)) {) so I can understand your naming conventions? Thanks for your help! –  Daniel Galletta Mar 4 at 8:59
    
@user1912319 The answer works on rows from a database in the same way. –  Jack Mar 4 at 10:00
    
I am not understanding the $rows as $row and then checking if $row is set. Do you mind expanding a little? –  Daniel Galletta Mar 7 at 3:24
    
@user1912319 I have added a few comments, hope it helps. –  Jack Mar 7 at 3:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.