Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: Array Questions

  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Array Questions

    Getting back into learning more of php, and have two questions regarding arrays. I have the following query and code to pull some data out of a database.
    PHP Code:
    $query mysql_query("SELECT id, owner, name FROM towns") or die(mysql_error());
        while(
    $row mysql_fetch_assoc($query)){
            
    $towns[] = $row;
        }
        
        echo 
    "<pre>";
        echo 
    print_r($towns);
        echo 
    "</pre>"
    And the above outputs the below:
    Code:
    Array
    (
        [0] => Array
            (
                [id] => 1
                [owner] => 1
                [name] => Zion
            )
    
        [1] => Array
            (
                [id] => 2
                [owner] => 2
                [name] => my town
            )
    
        [2] => Array
            (
                [id] => 3
                [owner] => 2
                [name] => testing town
            )
    
    )
    1
    My current questions are:

    a. How can I have array start at 1 and not 0.
    b. I'm unsure why I keep getting a value of 1 at the end of the array.

    Can anyone help?
    Thanks,
    PT

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by ptmuldoon View Post
    Getting back into learning more of php, and have two questions regarding arrays. I have the following query and code to pull some data out of a database.
    PHP Code:
    $query mysql_query("SELECT id, owner, name FROM towns") or die(mysql_error());
        while(
    $row mysql_fetch_assoc($query)){
            
    $towns[] = $row;
        }
        
        echo 
    "<pre>";
        echo 
    print_r($towns);
        echo 
    "</pre>"
    And the above outputs the below:
    Code:
    Array
    (
        [0] => Array
            (
                [id] => 1
                [owner] => 1
                [name] => Zion
            )
    
        [1] => Array
            (
                [id] => 2
                [owner] => 2
                [name] => my town
            )
    
        [2] => Array
            (
                [id] => 3
                [owner] => 2
                [name] => testing town
            )
    
    )
    1
    My current questions are:

    a. How can I have array start at 1 and not 0.
    assign your own key:
    PHP Code:
    $query mysql_query("SELECT id, owner, name FROM towns") or die(mysql_error());
            
    $i 1;
        while(
    $row mysql_fetch_assoc($query)){
            
    $towns[$i] = $row;
        }
        
        echo 
    "<pre>";
        echo 
    print_r($towns);
        echo 
    "</pre>"
    b. I'm unsure why I keep getting a value of 1 at the end of the array.
    because you print the result returned by print_r.
    PHP Code:
            $presult print_r($towns);
        echo 
    $presult
    print_r don't need echo, it is a kind of 'echo'

    regards

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by oesxyl View Post
    assign your own key:
    PHP Code:
    $query mysql_query("SELECT id, owner, name FROM towns") or die(mysql_error());
            
    $i 1;
        while(
    $row mysql_fetch_assoc($query)){
            
    $towns[$i] = $row;
        }
        
        echo 
    "<pre>";
        
    print_r($towns);
        echo 
    "</pre>"
    Thanks for the help. But assigning a key like mentioned above will only give me the second set of values in the multi-array, and not all 3 items. I'm not sure, but perhaps I can't do this with a while loop, but may need to use a for or foreach loop?

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by ptmuldoon View Post
    Thanks for the help. But assigning a key like mentioned above will only give me the second set of values in the multi-array, and not all 3 items. I'm not sure, but perhaps I can't do this with a while loop, but may need to use a for or foreach loop?
    PHP Code:
    $query mysql_query("SELECT id, owner, name FROM towns") or die(mysql_error());
            
    $i 1;
        while(
    $row mysql_fetch_assoc($query)){
            
    $towns[$i] = $row;
            
    $i++; // my fault, this was missing
        
    }
        
        echo 
    "<pre>";
        
    print_r($towns);
        echo 
    "</pre>"
    regards

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Thanks for the help, and along the same path as my earlier question. After I combine 2 array's using array_merge, how would have the array start at 1 again, instead of 0? So I have:
    PHP Code:
    $towns array_merge($default$subchilds); 
    And it outputs starting with [0], when I need to start at [1] for the a menu function I'm working with.
    Code:
    Array
    (
        [0] => Array
            (
                [text] => Articles
                [class] => articles
                [link] => #
                [show_condition] => 1
                [parent] => 0
            )
    
        [1] => Array
            (
                [text] => Users
                [class] => users
                [link] => #
                [show_condition] => 1
                [parent] => 0
            )
    
        [2] => Array
            (
                [text] => Groups
                [class] => groups
                [link] => #
                [show_condition] => 1
                [parent] => 0
            )
    
     ....... Down to array 10 plus
    )

  • #6
    New Coder
    Join Date
    Apr 2006
    Posts
    20
    Thanks
    2
    Thanked 1 Time in 1 Post
    Just out of curiosity, why do you need to start your array at 1? I usually find it easier to just remember to call $array[$i-1] (or something similar) if my count starts at 1, rather than trying to mess with array keys.

    --Josh

  • #7
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Well, I'm working with a dynamic drop down menu from here:

    http://www.roscripts.com/Building_a_..._menu-216.html

    And the code used in the menu creation has arrays starting at 1. I took a try at modifying the code to accept an array starting at 0, but can't seem to get it to work.

    And I'm basically working with the menu, pulling data from a database to create the array and menus.

    So if I could get the array to start with 1 after the array_merge, (merging the main and submenus) I think I'll pretty close to accomplishing the menu to work the way I would like with the proper submenu's pulled from the db.

  • #8
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by infinivert View Post
    I usually find it easier to just remember to call $array[$i-1] (or something similar) if my count starts at 1, rather than trying to mess with array keys.

    --Josh
    Josh. That got me thinking, and perhaps this is not the best way, but after merging the arrays, I simply made a new array based on the merged one shifting key + 1. Not sure if the below is the best method, but this is what I did (FYI. Its still a work in progress as I get the correct information pulled into the array):
    PHP Code:
    //Build Submenu Query for User Towns
        
    $query mysql_query("SELECT id, owner, name FROM towns") or die(mysql_error());
        
    $i 1;
        while(
    $row mysql_fetch_assoc($query)){
            
    $row['text'] = $row['name'];
            
    $row['class'] = 'articles';
            
    $row['link'] = 'towns.php&town='.$row['id'];
            
    $row['show_condition'] = True;
            
    $row['parent'] = 3;
            
    $submenu[] = $row;
            
    $i++;
        }
        
        
    $towns array_merge($default$submenu);
        
    $count count($towns);
        
    $x 1;
        while(
    $x <= $count){
            
    $menu[$x] = $towns[$x-1];
            
    $x++;
        } 


  •  

    Posting Permissions

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