4

I want to sum in my array :

<?php
if(isset($values[$key])) {
    $values[$key] += $total;
} else {
    $values[$key] = $total;
}

If I just write "+=", I have error "Undefined index". Do you know an easier way? Because is too long on a long code. Thanks

1
  • what is your $key?the error is your else. because its not set. Commented Feb 9, 2016 at 9:46

7 Answers 7

3

You can shorten it a bit

<?php
if(!isset($values[$key]))
  $values[$key]= 0;
$values[$key] += $total;

but the way you wrote the code already is a quite succinct and, more importantly, quite clean way.

edit: the error occurs in the first place because when writing $values[$key] += $total;, internally it is the same as $values[$key] = $values[$key] + $total - and when $value[$key] is not initiated in the first place, it can not be read.

PHP normally assumes that it is 0 then and throws the "key not defined"-note to notificate the programmer that he forgot to initiate an element of the array.

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

Comments

1

Try this:

<?php
$values[$key] = @$values[$key] + $total;

1 Comment

it should be noted that this does not solve the problem that causes the undefined index error but just omits the error message - which works, but is not really a "clean solution"
1

I think you can simply use array_sum($arrayname) function.

if you want to sum keys of array do something like this

array_sum(array_keys($array_name));

1 Comment

plus one for not trying to reinvent hot water.
1

You can do everything in one line:

$values[$key] = array_key_exists($key, $values) ? $values[$key] + $total : $total;

Here's the reference:

Comments

1

It should be the $total first

<?php 

$total = 0;

if(isset($values[$key])) {
   $total += $values[$key]; //Also equal to $total = $total + $values[$key];
} else {
    $total = $values[$key];
}

Comments

0

Another way to use less lines is to use a ternary operator:

<?php
$values[$key] = isset($values[$key]) ? ($values[$key] + $total) : $total;

Comments

0

I'm not entirely sure what you are trying to do, it would be quite nice to see the array you are passing into this. Either way the undefined index error is being produced because you are trying to access the array key $key when it does not exist.

From what I can work out, you just want to add the total to a key off your array if that key is set? If the key is not set then you want to create that key and set its value equal to the current total. At least that is my understanding of what you want.

So what you have done is almost correct except you try to access an index that hasn't been set when your conditional fails.

if (!isset($array[$key])) {
    $array[$key] = $total;
} else {
    $array[$key] += $total;
}

The above should fix your error, there probably are a few cleaner solutions but without knowing more information about the contents of the array and the purpose of the script it is hard to say what is the best method to use.

Comments

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.