0

I stuck here

i wanna sum total from checked checkbox value, and i use array

$check=$_POST[check];
$hit=count($check);
for($h=0;$h<=$hit-1;$h++) {
echo $check[$h];
//output without array : 1,2,3
//i wanna create array ex : $array = array('1', '2', '3');
//and sum total with echo array_sum($array);
//and total is 6
}

how to generate or create array from my loop ?

0

2 Answers 2

3

You say you want to create an array like $array = array('1', '2', '3');, but according to your code you already have that array, ie $check == array('1', '2', '3'); I'm not too clear on what your question is, but assuming that

$check is equal to $_POST[check] is equal to array('1', '2', '3');

then here is no need for count() or loops or creating another array. You just need one line:

$sum = array_sum($check); // 6

See demo

Sign up to request clarification or add additional context in comments.

Comments

0

You can get the sum like this -

$sum = '';
for($h=0;$h<$hit;$h++) {
    $sum += $check[$h];
}
echo $sum;

2 Comments

Awesome, work smooth!! Thank you so much :) Correct Answered for you
your welcome, you need to approve the answer then :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.