Jump to content

[SOLVED] array problem


xiao

Recommended Posts

I get this error: Warning: array_sum() [function.array-sum]: The argument should be an array in ...\index.php on line 71

 

everything works until $totaal = array_sum($ECprijs) + array_sum($prijs); (= line 71)

<?php
//Extra componenten declareren
function extraComponenten(){
$ECaantal = $_POST["aantalextra"];
for($i = 0;$i <= $ECaantal;$i++){
	$extracomponent = $_POST["extracomponent".$i];
	$ECtype[$i] = substr($extracomponent, 0, 6);
	$deel = explode(" - ", $extracomponent);
	$ECprijs[$i] = $deel[1];
}
}
//Prijs uit string halen
function prijs(){
$product[0] = $_POST["processor"];
$product[1] = $_POST["moederbord"];
$product[2] = $_POST["hardeschijf1"];
$product[3] = $_POST["hardeschijf2"];
$product[4] = $_POST["geheugen"];
$product[5] = $_POST["cddvdstation1"];
$product[6] = "AMD456 - 0,00"; //$product[6] = $_POST["cddvdstation2"];
$product[7] = $_POST["grafischekaart"];
$product[8] = $_POST["voeding"];
$product[9] = $_POST["tvkaart"];
$product[10] = $_POST["geluidskaart"];
$product[11] = $_POST["netwerkkaart"];
$product[12] = $_POST["diskettestation"];
$product[13] = $_POST["kaartlezer"];
$product[14] = $_POST["monitor"];
for($i = 0;$i < 15;$i++){
	$deel = explode(" - ", $product[$i]);
	$prijs[$i] = $deel[1];
}

$geheugenaantal = $_POST["geheugenaantal"];

$prijs[4] = $prijs[4] * $geheugenaantal;
echo $prijs[4];
}
//Totaal berekenen
function totaalBerekenen(){
extraComponenten();
prijs();
$totaal = array_sum($ECprijs) + array_sum($prijs);
echo $totaal;
}

if(isset($_POST["totaal"])){
totaalBerekenen();	
}
?>

 

Sorry for the dutch code.

I tried using global, but that didn't help, or I'm doing it wrong.

Both arrays are being filled with values.

Link to comment
https://forums.phpfreaks.com/topic/99556-solved-array-problem/
Share on other sites

Have your first two functions return their respective arrays. Otherwise the variables are undefined:

 

<?php
//Extra componenten declareren
function extraComponenten(){
$ECaantal = $_POST["aantalextra"];
for($i = 0;$i <= $ECaantal;$i++){
	$extracomponent = $_POST["extracomponent".$i];
	$ECtype[$i] = substr($extracomponent, 0, 6);
	$deel = explode(" - ", $extracomponent);
	$ECprijs[$i] = $deel[1];
	return $ECprijs;
}
}
//Prijs uit string halen
function prijs(){
$product[0] = $_POST["processor"];
$product[1] = $_POST["moederbord"];
$product[2] = $_POST["hardeschijf1"];
$product[3] = $_POST["hardeschijf2"];
$product[4] = $_POST["geheugen"];
$product[5] = $_POST["cddvdstation1"];
$product[6] = "AMD456 - 0,00"; //$product[6] = $_POST["cddvdstation2"];
$product[7] = $_POST["grafischekaart"];
$product[8] = $_POST["voeding"];
$product[9] = $_POST["tvkaart"];
$product[10] = $_POST["geluidskaart"];
$product[11] = $_POST["netwerkkaart"];
$product[12] = $_POST["diskettestation"];
$product[13] = $_POST["kaartlezer"];
$product[14] = $_POST["monitor"];
for($i = 0;$i < 15;$i++){
	$deel = explode(" - ", $product[$i]);
	$prijs[$i] = $deel[1];
}

$geheugenaantal = $_POST["geheugenaantal"];

$prijs[4] = $prijs[4] * $geheugenaantal;
echo $prijs[4];
return $prijs
}
//Totaal berekenen
function totaalBerekenen(){
$ECprijs = extraComponenten();
$$prijs = prijs();
$totaal = array_sum($ECprijs) + array_sum($prijs);
echo $totaal;
}

if(isset($_POST["totaal"])){
totaalBerekenen();	
}
?>

Link to comment
https://forums.phpfreaks.com/topic/99556-solved-array-problem/#findComment-509291
Share on other sites

Sorry, there was a stray dollar sign in there. Try:

 

<?php
//Extra componenten declareren
function extraComponenten(){
$ECaantal = $_POST["aantalextra"];
for($i = 0;$i <= $ECaantal;$i++){
	$extracomponent = $_POST["extracomponent".$i];
	$ECtype[$i] = substr($extracomponent, 0, 6);
	$deel = explode(" - ", $extracomponent);
	$ECprijs[$i] = $deel[1];
	return $ECprijs;
}
}
//Prijs uit string halen
function prijs(){
$product[0] = $_POST["processor"];
$product[1] = $_POST["moederbord"];
$product[2] = $_POST["hardeschijf1"];
$product[3] = $_POST["hardeschijf2"];
$product[4] = $_POST["geheugen"];
$product[5] = $_POST["cddvdstation1"];
$product[6] = "AMD456 - 0,00"; //$product[6] = $_POST["cddvdstation2"];
$product[7] = $_POST["grafischekaart"];
$product[8] = $_POST["voeding"];
$product[9] = $_POST["tvkaart"];
$product[10] = $_POST["geluidskaart"];
$product[11] = $_POST["netwerkkaart"];
$product[12] = $_POST["diskettestation"];
$product[13] = $_POST["kaartlezer"];
$product[14] = $_POST["monitor"];
for($i = 0;$i < 15;$i++){
	$deel = explode(" - ", $product[$i]);
	$prijs[$i] = $deel[1];
}

$geheugenaantal = $_POST["geheugenaantal"];

$prijs[4] = $prijs[4] * $geheugenaantal;
echo $prijs[4];
return $prijs;
}
//Totaal berekenen
function totaalBerekenen(){
$ECprijs = extraComponenten();
$prijs = prijs();
$totaal = array_sum($ECprijs) + array_sum($prijs);
echo $totaal;
}

if(isset($_POST["totaal"])){
totaalBerekenen();	
}
?>

Link to comment
https://forums.phpfreaks.com/topic/99556-solved-array-problem/#findComment-509309
Share on other sites

That's because the decimal separator that you need to use is a point and not a comma.

 

PHP doesn't recognise the elements of your array as numbers, so tries to convert them to a number. What it ends up doing is taking everything up to the first non-numeric character as being the number.

Link to comment
https://forums.phpfreaks.com/topic/99556-solved-array-problem/#findComment-509383
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.