ptmuldoon
01-23-2008, 07:03 PM
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?
while($plyrs = mysql_fetch_assoc($query)){;
echo '<pre>';
print_r($plyrs);
echo '</pre>';
}
ptmuldoon
01-23-2008, 07:17 PM
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;
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?
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
)
kbluhm
01-23-2008, 07:51 PM
$plyrs = array();
while ( $plyr = mysql_fetch_assoc( $query ) )
{
$plyrs[] = $plyr;
}
print_r( $plyrs );
ptmuldoon
01-23-2008, 09:50 PM
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?
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
)
)
)