Jump to content

array_sum


Porl123

Recommended Posts

I have this blackjack script I've just started and I've had no problems except this array_sum gives off an error message and I can't see why. The system I have now is it takes the players cards that are in one field of this table and separated with a comma, then I class it was it's value is on the other end, as well as the aces

 

<?php
$sql = mysql_query("SELECT * FROM `bj_games` WHERE `username` = '$_SESSION[username]'");
$row = mysql_fetch_array($sql);
$play_cards = explode(',', $row['playercards']);
foreach($play_cards as $value) {
	$face = is_numeric($value);
	if($face >= 2 && $face <= 10) {
		$total[] = $face;
	}
	if($face > 10 && $face < 14) {
		$total[] = 10;
	}
	if($face == 14) {
		if((array_sum($total) + 11) < 22) {
			$total[] = 11;
		} else {
			$total[] = 1;
		}
	}
}
$total_sum = array_sum($total);
?>

 

Here's my code and here's the error message I get

 

Warning: array_sum() [function.array-sum]: The argument should be an array in /home/paul/public_html/blackjack.php on line 108

 

If you can tell me where I've gone wrong here it'd be much appreciated! :)

Link to comment
https://forums.phpfreaks.com/topic/126851-array_sum/
Share on other sites

Wait more than 1 hour to get impatient.

 

I would initialize $total as an empty array, so you know you have an array at the end even if nothing goes into it.

 

<?php
        $total =array();
$sql = mysql_query("SELECT * FROM `bj_games` WHERE `username` = '$_SESSION[username]'");
$row = mysql_fetch_array($sql);
$play_cards = explode(',', $row['playercards']);
foreach($play_cards as $value) {
	$face = is_numeric($value);
	if($face >= 2 && $face <= 10) {
		$total[] = $face;
	}
	if($face > 10 && $face < 14) {
		$total[] = 10;
	}
	if($face == 14) {
		if((array_sum($total) + 11) < 22) {
			$total[] = 11;
		} else {
			$total[] = 1;
		}
	}
}
        if (!empty($total)) 
  	      $total_sum = array_sum($total);
        else
              $total_sum = 0;
?>

 

Ken

 

 

Link to comment
https://forums.phpfreaks.com/topic/126851-array_sum/#findComment-656127
Share on other sites

What is $face = is_numeric($value); supposed to do? is_numeric() returns true or false depending on if the variable you supply as the argument is a number or numeric string.

 

Of course it won't add any totals because throughout the rest of your script you are checking to see if a bool is greater than or less than a number, which won't work.

 

Maybe re-think what you are trying to do? I don't see why you are setting $face to a bool anyway.

Link to comment
https://forums.phpfreaks.com/topic/126851-array_sum/#findComment-656154
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.