SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating a multidimensional array from a database

    I want to put the result of a query into a multidimensional array eg
    from a query like
    Code:
    $query = "SELECT roomName,roomNumber FROM rooms";
    how can I get the results into an array that is like
    Code:
    <?php
    $rooms = array(
      'Studio'=>array(
        '101',
        '102',
        '103'
      ),
      '1 Bedroom'=>array(
        '201',
        '202',
        '203'
      );
    ?>

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    72
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know what your content of that table is? You can use a while and add the results to an array..

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The content is like I have put in the second code block and how do you put it into a multidimensional array is what I am asking???

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2002
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this should work:

    PHP Code:
    $query "SELECT roomName,roomNumber FROM rooms";

    $result $db->Query($query);

    while(
    $room $db->FetchArray($result)){
        
    $rooms[$room['roomName']][] = $room['roomNumber'];

    The db stuff won't be what you want, you'll need to replace those with the mysql/postgresql/your database specific calls.

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2005
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks ..that did it


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •