Jump to content

Trouble with array


slipvayne

Recommended Posts

I've never used array's because I've always found a way to do something without them. I have finally taught myself how array's work, but I cannot seem to add a key and value to an array without my code creating another array inside that one. I'm not sure what I'm doing wrong, but this is what I'm getting.

 

From what I understand, print_r is suppose to print out an array. This is what my print_r displays:

 

Array ( [knife] => 2000 ) Array ( [baseballbat] => 2000 [knife] => 2000 ) Array ( [colt] => 2000 [knife] => 2000 [baseballbat] => 2000 ) Array ( [deserteagle] => 2000 [baseballbat] => 2000 [knife] => 2000 [colt] => 2000 ) Array ( [ingram] => 2000 [colt] => 2000 [knife] => 2000 [baseballbat] => 2000 [deserteagle] => 2000 ) Array ( [deserteagle] => 2000 [ump40] => 2000 [baseballbat] => 2000 [knife] => 2000 [colt] => 2000 [ingram] => 2000 ) Array ( [colt] => 2000 [ingram] => 2000 [deserteagle] => 2000 [knife] => 2000 [baseballbat] => 2000 [ump40] => 2000 [grenades] => 958 ) Array ( [baseballbat] => 2000 [ump40] => 2000 [colt] => 2000 [knife] => 2000 [deserteagle] => 2000 [ingram] => 2000 [grenades] => 958 ) Array ( [deserteagle] => 2000 [ingram] => 2000 [baseballbat] => 2000 [knife] => 2000 [colt] => 2000 [ump40] => 2000 [grenades] => 958 ) Array ( [colt] => 2000 [ump40] => 2000 [deserteagle] => 2000 [knife] => 2000 [baseballbat] => 2000 [ingram] => 2000 [grenades] => 958 ) Array ( [baseballbat] => 2000 [ingram] => 2000 [colt] => 2000 [knife] => 2000 [deserteagle] => 2000 [ump40] => 2000 [grenades] => 958 ) Array ( [deserteagle] => 2000 [ump40] => 2000 [baseballbat] => 2000 [knife] => 2000 [colt] => 2000 [ingram] => 2000 [grenades] => 958 ) Array ( [colt] => 2000 [ingram] => 2000 [deserteagle] => 2000 [knife] => 2000 [baseballbat] => 2000 [ump40] => 2000 [grenades] => 958 ) Array ( [baseballbat] => 2000 [ump40] => 2000 [colt] => 2000 [knife] => 2000 [deserteagle] => 2000 [ingram] => 2000 [grenades] => 958 ) Array ( [deserteagle] => 2000 [ingram] => 2000 [baseballbat] => 2000 [knife] => 2000 [colt] => 2000 [ump40] => 2000 [grenades] => 958 ) Array ( [ump40] => 2000 [pepperspray] => 2000 [colt] => 2000 [knife] => 2000 [ingram] => 2000 [baseballbat] => 2000 [deserteagle] => 2000 [grenades] => 958 ) Array ( [baseballbat] => 2000 [deserteagle] => 2000 [flashbangs] => 2000 [ump40] => 2000 [ingram] => 2000 [pepperspray] => 2000 [colt] => 2000 [knife] => 2000 [grenades] => 958 ) Array ( [pepperspray] => 2000 [colt] => 2000 [knife] => 2000 [baseballbat] => 2000 [ingram] => 2000 [ump40] => 2000 [deserteagle] => 2000 [flashbangs] => 2000 [teargas] => 1800 [grenades] => 958 ) Array ( [pepperspray] => 2000 [deserteagle] => 2000 [flashbangs] => 2000 [ingram] => 2000 [ump40] => 2000 [baseballbat] => 2000 [colt] => 2000 [knife] => 2000 [tasergun] => 1800 [teargas] => 1800 [grenades] => 958 ) Array ( [pepperspray] => 2000 [colt] => 2000 [knife] => 2000 [ump40] => 2000 [baseballbat] => 2000 [ingram] => 2000 [deserteagle] => 2000 [flashbangs] => 2000 [teargas] => 1800 [tasergun] => 1800 [combathelmet] => 1600 [grenades] => 958 ) Array ( [ingram] => 2000 [pepperspray] => 2000 [flashbangs] => 2000 [baseballbat] => 2000 [deserteagle] => 2000 [ump40] => 2000 [colt] => 2000 [knife] => 2000 [tasergun] => 1800 [teargas] => 1800 [combathelmet] => 1600 [grenades] => 958 [kevlarvest] => 271 ) Array ( [ump40] => 2000 [ingram] => 2000 [knife] => 2000 [deserteagle] => 2000 [colt] => 2000 [baseballbat] => 2000 [pepperspray] => 2000 [flashbangs] => 2000 [teargas] => 1800 [tasergun] => 1800 [combathelmet] => 1600 [grenades] => 958 [kevlarvest] => 271 ) Array ( [baseballbat] => 2000 [ump40] => 2000 [flashbangs] => 2000 [colt] => 2000 [pepperspray] => 2000 [deserteagle] => 2000 [ingram] => 2000 [knife] => 2000 [tasergun] => 1800 [teargas] => 1800 [combathelmet] => 1600 [grenades] => 958 [kevlarvest] => 271 ) Array ( [deserteagle] => 2000 [baseballbat] => 2000 [knife] => 2000 [pepperspray] => 2000 [ingram] => 2000 [colt] => 2000 [ump40] => 2000 [flashbangs] => 2000 [teargas] => 1800 [tasergun] => 1800 [combathelmet] => 1600 [grenades] => 958 [kevlarvest] => 271 )

 

Notice how the first array has one key/value, the second has the same value as the first plus an additional value. The third array has the same values as the second array plus an additional value. I'm confused as to whether my array is correct, and that print_t is just showing me how the array has changed everytime the while loop went through, or there is an error in my code and my array is not correct. I want to get this cleared before I continue. Here is my code so far:

 


<?

$user = $_GET['user'];
$attacks = $_GET['attacks'];


$quser = mysql_query("SELECT * FROM user WHERE username = '$user'");
$nuser = mysql_num_rows($quser); 

if ($nuser == 1) {

$ruser = mysql_fetch_array($quser);

$totalpower = $ruser[power] + $ruser[defense];
$sabpower = round ($totalpower * $sabdmg);

$ramount = mysql_fetch_array(mysql_query("SELECT * FROM user_items WHERE username = '$user'"));

$wa = array();

	$qweps = mysql_query("SELECT * FROM weaponstore ORDER BY type ASC, price ASC");
	while ($rweps = mysql_fetch_array($qweps)) {

		$wepname = $rweps[colname];
		$wepamount = $ramount[$wepname];

		if ($wepamount >= 1) { $wa[$wepname] = $wepamount; }

		arsort($wa);
		print_r($wa);



	}

}


?>

 

Basically the code should add the $wepname as the key and $wepamount as the value to an array $wa only if $wepamount is >= 1, but the way my print_r is showing it I'm just confused as to why I'm getting a bunch of arrays and not just one.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.