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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    array_merge help please

    How do you combine all the rows of a query into one array? I have the following that will put each row into its own separate array. Is there a way to merge them all together, or to create an array of all the rows?

    PHP Code:
    while($plyrs mysql_fetch_assoc($query)){; 
            
            echo 
    '<pre>';
            
    print_r($plyrs);
            echo 
    '</pre>';    
        } 

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Actually, after I posted the above, I was able to work out the following, and its pretty close. Maybe I'm finally starting to get the hang of this.

    Anyway, this is pretty close;

    PHP Code:
    for($i =0$i mysql_num_rows($query); $i++){;
            
    $plyrs[$i] = mysql_fetch_assoc($query); 
            
    $cplyrs array_merge($plyrs,$plyrs[$i]);
        }
            echo 
    '<pre>';
            
    print_r($cplyrs);
            echo 
    '</pre>'
    But it is giving one extra piece at the bottom, a duplicate actually. I think it has to do with the array_merge above, and $plyrs not actually being defined as an array?

    Code:
    Array
    (
        [0] => Array
            (
                [pid] => 1
                [pname] => 0
                [pcolor] => brown
            )
    
        [1] => Array
            (
                [pid] => 2
                [pname] => GunSlinger
                [pcolor] => blue
            )
    
        [2] => Array
            (
                [pid] => 3
                [pname] => Aodance
                [pcolor] => teal
            )
    
        [3] => Array
            (
                [pid] => 4
                [pname] => rnocb
                [pcolor] => green
            )
    
        [4] => Array
            (
                [pid] => 5
                [pname] => Phatdragon
                [pcolor] => red
            )
    
        [5] => Array
            (
                [pid] => 6
                [pname] => mamoure
                [pcolor] => purple
            )
    
        [6] => Array
            (
                [pid] => 7
                [pname] => charger
                [pcolor] => grey
            )
    
        [7] => Array
            (
                [pid] => 8
                [pname] => The Admiral
                [pcolor] => black
            )
    
        [8] => Array
            (
                [pid] => 9
                [pname] => BradH
                [pcolor] => yellow
            )
    
        [pid] => 9
        [pname] => BradH
        [pcolor] => yellow
    )

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    PHP Code:
    $plyrs = array();

    while ( 
    $plyr mysql_fetch_assoc$query ) )

        
    $plyrs[] = $plyr
    }

    print_r$plyrs ); 

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Thanks

    I needed to modify that a little to be able to include a key. The key was also necessary to be able to merge it correctly with another array. so now I have the following. Can someone maybe explain how I would search the [plyrs] array to find a specific player? I think this is called a multi-dimensional array?

    PHP Code:
    Array
    (
        [
    gid] => 1134
        
    [date] => 01/05 12/23
        
    [plyrs] => Array
            (
                [
    0] => Array
                    (
                        [
    gid] => 1134
                        
    [pid] => 2
                        
    [pname] => GunSlinger
                        
    [pcolor] => blue
                        
    [pstate] => inactive
                    
    )

                [
    1] => Array
                    (
                        [
    gid] => 1134
                        
    [pid] => 3
                        
    [pname] => Aodance
                        
    [pcolor] => teal
                        
    [pstate] => inactive
                    
    )

                [
    2] => Array
                    (
                        [
    gid] => 1134
                        
    [pid] => 4
                        
    [pname] => rnocb
                        
    [pcolor] => green
                        
    [pstate] => trading
                    
    )
            )


    Last edited by ptmuldoon; 01-23-2008 at 10:07 PM.


  •  

    Posting Permissions

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