Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 603239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:54:33+00:00 2026-05-13T16:54:33+00:00

I want to make a multidimensional array in php. Here is what i have

  • 0

I want to make a multidimensional array in php. Here is what i have done:

Firstly, i have 3 tables:

entreprise:

enterprise_id         name
1                      e1
2                      e2

site:

site_id              entreprise_id             name
1                       1                     e1_site1
2                       2                     e2_site1
...

salarie:

salarie_id           site_id                   name
1                       1                    e1_site1_salarie1
2                       2                    e2_site1_salarie1
...

I have the following PHP code:

$query = "select * from entreprise";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){

    $query2 = "select * from site where entreprise_id = $row[entreprise_id]";
    $result2 = mysql_query($query2);
    $a2 = array();
    while($row2 = mysql_fetch_assoc($result2)){

        $query3 = "select * from salarie where site_id = $row2[site_id]";
        $result3 = mysql_query($query3);

        while($row3 = mysql_fetch_assoc($result3)){
            $a3[] = array("text"=>$row3[nom]);
        }

        $a2[] = array("text"=>$row2[nom],'children'=>$a3);
    }
        $a1[] = array("text"=>$row[id]." ".$row[nom],'children'=>$a2);
}

But you can see the ouput is mixed. For example, the ‘e1_site1_salarie1_nom’ is in ‘e1_site2’, the ‘e1_site1_salarie1_nom’ is under ‘e2_site2’. It’s strange.

Array
(
    [0] => Array
        (
            [text] =>  e1
            [children] => Array
                (
                    [0] => Array
                        (
                            [text] => e1_site1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [text] => e1_site1_salarie1_nom
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [text] => e1_site2
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [text] => e1_site1_salarie1_nom
                                        )

                                    [1] => Array
                                        (
                                            [text] => e1_site2_sa1
                                        )

                                    [2] => Array
                                        (
                                            [text] => e1_site2_sa2
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [text] =>  e2
            [children] => Array
                (
                    [0] => Array
                        (
                            [text] => e2_site2
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [text] => e1_site1_salarie1_nom
                                        )

                                    [1] => Array
                                        (
                                            [text] => e1_site2_sa1
                                        )

                                    [2] => Array
                                        (
                                            [text] => e1_site2_sa2
                                        )

                                    [3] => Array
                                        (
                                            [text] => e2_site2_salarie2_nom
                                        )

                                )

                        )

                )

        )

)

I think it’s the problem of my php code. I think i should use more conditional judgement such as if else etc with the following code.

        $a2[] = array("text"=>$row2[nom],'children'=>$a3);

        $a1[] = array("text"=>$row[id]." ".$row[nom],'children'=>$a2);

But I don’t know how to change it.

Do you have any clue or suggestions?

Thanks in advance.

Edit:

Following Tatu’s suggestion, it works. Thanks Tatu. Now i have one more question. I don’t want the [1] => Array in the header of the arrays. How to achieve that?

Array
(
    [1] => Array
        (
            [text] => e1
            [children] => Array
                (
                    [1] => Array
                        (
                            [text] => e1_site1
                            [children] => Array
                                (
                                    [1] => e1_site1_salarie1_nom
                                )

                        )

                    [3] => Array
                        (
                            [text] => e1_site2
                            [children] => Array
                                (
                                    [3] => e1_site2_sa1
                                    [4] => e1_site2_sa2
                                )

                        )

                )

        )

    [2] => Array
        (
            [text] => e2
            [children] => Array
                (
                    [2] => Array
                        (
                            [text] => e2_site2
                            [children] => Array
                                (
                                    [2] => e2_site2_salarie2_nom
                                )

                        )

                )

        )

)

You can see the json code:

{"1":{"text":"e1","children":{"1":{"text":"e1_site1","children":{"1":"e1_site1_salarie1_nom"}},"3":{"text":"e1_site2","children":{"3":"e1_site2_sa1","4":"e1_site2_sa2"}}}},"2":{"text":"e2","children":{"2":{"text":"e2_site2","children":{"2":"e2_site2_salarie2_nom"}}}}} 

I want the result to be like this:

{{"text":"e1","children":{{"text":"e1_site1","children":{"text":"e1_site1_salarie1_nom"}},{"text":"e1_site2","children":{"text":"e1_site2_sa1","text":"e1_site2_sa2"}}}},{"text":"e2","children":{{"text":"e2_site2","children":{"text":"e2_site2_salarie2_nom"}}}}} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:54:33+00:00Added an answer on May 13, 2026 at 4:54 pm

    Don’t do nested queries, they are completely unnecessary and only slow things down. Get the values first and combine them later. This might work:

    $enterprise_q = mysql_query("select * from entreprise");
    $site_q = mysql_query("select * from site");
    $salarie_q = mysql_query("select * from salarie");
    
    $result = array();
    
    $enterprise = array();
    $site = array();
    $salarie = array();
    
    # Now loop through the results 'top down',
    # starting from salaries and indexing by site_id.
    while($row = mysql_fetch_assoc($salarie_q)) {
        # Initialize variables properly
        if(!is_array($salarie[$row['site_id']])) {
            $salarie[$row['site_id']] = array();
        }
    
        $salarie[$row['site_id']][$row['salarie_id']] = $row['name'];
    }
    
    # Loop through sites, store sites and associated salaries by enterprise_id
    while($row = mysql_fetch_assoc($site_q)) {
        if(!is_array($site[$row['enterprise_id']])) {
            $site[$row['enterprise_id']] = array();
        }
    
        $site[$row['enterprise_id']][$row['site_id']] = array(
            'name' => $row['name'],
            'salarie' => $salarie[$row['site_id']]
        );
    }
    
    # Loop through enterprises and gather all site data
    while($row = mysql_fetch_assoc($enterprice_q)) {
        if(!is_array($enterprise[$row['enterprise_id']])) {
            $enterprise[$row['enterprise_id']] = array();
        }
    
        $enterprise[$row['enterprise_id']] = array(
            'name' => $row['name'],
            'sites' => $site[$row['enterprise_id']];
        );
    }
    

    This is not an optimal way to handle it, but it is better than your current. This should result in an array something like this:

    $enterprise = array(
        1 => array(
            'name' => 'e1',
            'sites' => array(
                1 => array(
                    'name' => 'e1_site1',
                    'salarie' => array(
                        1 => 'e1_site1_salarie1',
                        1 => 'e1_site1_salarie2',
                        1 => 'e1_site1_salarie3'
                    )
                ),
                2 => array(
                    'name' => 'e1_site2',
                    'salarie' => array(
                        1 => 'e1_site2_salarie1',
                        1 => 'e1_site2_salarie2'
                    )
                )
            )
        ),
        2 => array(
            'name' => 'e2',
            'sites' => array(
                1 => array(
                    'name' => 'e2_site1',
                    'salarie' => array(
                        1 => 'e2_site1_salarie1'
                    )
                )
            )
        )
    )
    

    Which should be what you’re looking for. I haven’t tested this so don’t expect it to work on first try, but this should at least give you some ideas.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 365k
  • Answers 365k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use the data in the /proc filesystem to… May 14, 2026 at 3:57 pm
  • Editorial Team
    Editorial Team added an answer Do you still get the error when you use a… May 14, 2026 at 3:57 pm
  • Editorial Team
    Editorial Team added an answer You can use array_chunk to create a single array comprised… May 14, 2026 at 3:57 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.